View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.referential.unit.local;
2   
3   /*
4    * #%L
5    * Reef DB :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import fr.ifremer.quadrige3.core.dao.referential.StatusCode;
27  import fr.ifremer.quadrige3.ui.swing.table.SwingTable;
28  import fr.ifremer.reefdb.dao.technical.Daos;
29  import fr.ifremer.reefdb.decorator.DecoratorService;
30  import fr.ifremer.reefdb.dto.ReefDbBeans;
31  import fr.ifremer.reefdb.dto.referential.UnitDTO;
32  import fr.ifremer.reefdb.service.StatusFilter;
33  import fr.ifremer.reefdb.ui.swing.content.manage.referential.unit.menu.ReferentialUnitsMenuUIModel;
34  import fr.ifremer.reefdb.ui.swing.content.manage.referential.unit.table.UnitTableModel;
35  import fr.ifremer.reefdb.ui.swing.content.manage.referential.unit.table.UnitTableRowModel;
36  import fr.ifremer.reefdb.ui.swing.util.table.AbstractReefDbTableModel;
37  import fr.ifremer.reefdb.ui.swing.util.table.AbstractReefDbTableUIHandler;
38  import org.apache.commons.collections4.CollectionUtils;
39  import org.apache.commons.lang3.StringUtils;
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  import org.jdesktop.swingx.table.TableColumnExt;
43  
44  import java.util.Collection;
45  import java.util.List;
46  
47  import static org.nuiton.i18n.I18n.t;
48  
49  /**
50   * Controlleur pour l administration des Units au niveau local
51   */
52  public class ReferentialUnitsLocalUIHandler extends AbstractReefDbTableUIHandler<UnitTableRowModel, ReferentialUnitsLocalUIModel, ReferentialUnitsLocalUI> {
53  
54      /**
55       * Logger.
56       */
57      private static final Log LOG = LogFactory.getLog(ReferentialUnitsLocalUIHandler.class);
58  
59      /** {@inheritDoc} */
60      @Override
61      public void beforeInit(final ReferentialUnitsLocalUI ui) {
62          super.beforeInit(ui);
63  
64          // create model and register to the JAXX context
65          final ReferentialUnitsLocalUIModel model = new ReferentialUnitsLocalUIModel();
66          ui.setContextValue(model);
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public void afterInit(final ReferentialUnitsLocalUI ui) {
72          initUI(ui);
73  
74          // Initialisation du tableau
75          initTable();
76  
77          // Enabled elements
78          getUI().getReferentialUnitsLocalTableDeleteBouton().setEnabled(false);
79          getUI().getReferentialUnitsLocalTableReplaceBouton().setEnabled(false);
80  
81          ReferentialUnitsMenuUIModel menuUIModel = getUI().getMenuUI().getModel();
82          menuUIModel.setLocal(true);
83  
84          menuUIModel.addPropertyChangeListener(ReferentialUnitsMenuUIModel.PROPERTY_RESULTS, evt -> loadUnits((Collection<UnitDTO>) evt.getNewValue()));
85  
86      }
87  
88      /**
89       * Initialisation du tableau.
90       */
91      private void initTable() {
92  
93          // mnemonic
94          final TableColumnExt mnemonicCol = addColumn(UnitTableModel.NAME);
95          mnemonicCol.setSortable(true);
96  
97          // Symbol
98          final TableColumnExt symbolCol = addColumn(UnitTableModel.SYMBOL);
99          symbolCol.setSortable(true);
100 
101         // Comment, creation and update dates
102         addCommentColumn(UnitTableModel.COMMENT);
103         TableColumnExt creationDateCol = addDatePickerColumnToModel(UnitTableModel.CREATION_DATE, getConfig().getDateTimeFormat(), false);
104         fixColumnWidth(creationDateCol, 120);
105         TableColumnExt updateDateCol = addDatePickerColumnToModel(UnitTableModel.UPDATE_DATE, getConfig().getDateTimeFormat(), false);
106         fixColumnWidth(updateDateCol, 120);
107 
108         // status
109         final TableColumnExt statusCol = addFilterableComboDataColumnToModel(
110                 UnitTableModel.STATUS,
111                 getContext().getReferentialService().getStatus(StatusFilter.LOCAL),
112                 false);
113         statusCol.setSortable(true);
114 
115         final UnitTableModel tableModel = new UnitTableModel(getTable().getColumnModel(), true);
116         getTable().setModel(tableModel);
117 
118         // Add extraction action
119         addExportToCSVAction(t("reefdb.property.units.local"));
120 
121         // Initialisation du tableau
122         initTable(getTable());
123 
124         creationDateCol.setVisible(false);
125         updateDateCol.setVisible(false);
126 
127         getTable().setVisibleRowCount(5);
128     }
129 
130     /** {@inheritDoc} */
131     @Override
132     public AbstractReefDbTableModel<UnitTableRowModel> getTableModel() {
133         return (UnitTableModel) getTable().getModel();
134     }
135 
136     /** {@inheritDoc} */
137     @Override
138     public SwingTable getTable() {
139         return getUI().getReferentialUnitsLocalTable();
140     }
141 
142     /** {@inheritDoc} */
143     @Override
144     protected void onRowsAdded(List<UnitTableRowModel> addedRows) {
145         super.onRowsAdded(addedRows);
146 
147         if (addedRows.size() == 1) {
148             UnitTableRowModel rowModel = addedRows.get(0);
149 
150             // Set default status
151             rowModel.setStatus(Daos.getStatus(StatusCode.LOCAL_ENABLE));
152 
153             setFocusOnCell(rowModel);
154         }
155     }
156 
157     /** {@inheritDoc} */
158     @Override
159     protected void onRowModified(int rowIndex, UnitTableRowModel row, String propertyName, Integer propertyIndex, Object oldValue, Object newValue) {
160         super.onRowModified(rowIndex, row, propertyName, propertyIndex, oldValue, newValue);
161         row.setDirty(true);
162         forceRevalidateModel();
163     }
164 
165     /** {@inheritDoc} */
166     @Override
167     protected boolean isRowValid(UnitTableRowModel row) {
168         row.getErrors().clear();
169         return !row.isEditable() || (super.isRowValid(row) && isUnique(row));
170     }
171 
172     private boolean isUnique(UnitTableRowModel row) {
173 
174         if (StringUtils.isNotBlank(row.getName()) && StringUtils.isNotBlank(row.getSymbol())) {
175             boolean duplicateFound = false;
176 
177             // check unicity in local table
178             for (UnitTableRowModel otherRow : getModel().getRows()) {
179                 if (row == otherRow) continue;
180                 if (row.getName().equalsIgnoreCase(otherRow.getName()) && row.getSymbol().equalsIgnoreCase(otherRow.getSymbol())) {
181                     // duplicate found in table
182                     ReefDbBeans.addError(row,
183                             t("reefdb.error.alreadyExists.referential",
184                                     t("reefdb.property.pmfm.unit"), decorate(row, DecoratorService.WITH_SYMBOL), t("reefdb.property.referential.local")),
185                             UnitTableRowModel.PROPERTY_NAME,
186                             UnitTableRowModel.PROPERTY_SYMBOL);
187                     duplicateFound = true;
188                     break;
189                 }
190             }
191 
192             if (!duplicateFound) {
193                 // check unicity in database
194                 List<UnitDTO> existingUnits = getContext().getReferentialService().getUnits(StatusFilter.ALL);
195                 if (CollectionUtils.isNotEmpty(existingUnits)) {
196                     for (UnitDTO unit : existingUnits) {
197                         if (!unit.getId().equals(row.getId())
198                                 && row.getName().equalsIgnoreCase(unit.getName())
199                                 && row.getSymbol().equalsIgnoreCase(unit.getSymbol())) {
200                             // duplicate found in database
201                             ReefDbBeans.addError(row,
202                                     t("reefdb.error.alreadyExists.referential",
203                                             t("reefdb.property.pmfm.unit"), decorate(row, DecoratorService.WITH_SYMBOL),
204                                             ReefDbBeans.isLocalStatus(unit.getStatus()) ? t("reefdb.property.referential.local") : t("reefdb.property.referential.national")),
205                                     UnitTableRowModel.PROPERTY_NAME,
206                                     UnitTableRowModel.PROPERTY_SYMBOL);
207                             break;
208                         }
209                     }
210                 }
211             }
212         }
213 
214         return row.getErrors().isEmpty();
215     }
216 
217     /**
218      * Load units.
219      *
220      * @param units a {@link java.util.Collection} object.
221      */
222     public void loadUnits(Collection<UnitDTO> units) {
223         getModel().setBeans(units);
224 
225         // Model not modify
226         getModel().setModify(false);
227     }
228 
229 }