View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.referential.pmfm.method.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.dto.ReefDbBeans;
30  import fr.ifremer.reefdb.dto.referential.pmfm.MethodDTO;
31  import fr.ifremer.reefdb.service.StatusFilter;
32  import fr.ifremer.reefdb.ui.swing.content.manage.referential.pmfm.method.menu.ManageMethodsMenuUIModel;
33  import fr.ifremer.reefdb.ui.swing.content.manage.referential.pmfm.method.table.MethodsTableModel;
34  import fr.ifremer.reefdb.ui.swing.content.manage.referential.pmfm.method.table.MethodsTableRowModel;
35  import fr.ifremer.reefdb.ui.swing.util.table.AbstractReefDbTableModel;
36  import fr.ifremer.reefdb.ui.swing.util.table.AbstractReefDbTableUIHandler;
37  import org.apache.commons.collections4.CollectionUtils;
38  import org.apache.commons.lang3.StringUtils;
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  import org.jdesktop.swingx.table.TableColumnExt;
42  
43  import java.util.List;
44  
45  import static org.nuiton.i18n.I18n.t;
46  
47  /**
48   * Controlleur pour l administration des Methods au niveau local
49   */
50  public class ManageMethodsLocalUIHandler extends AbstractReefDbTableUIHandler<MethodsTableRowModel, ManageMethodsLocalUIModel, ManageMethodsLocalUI> {
51  
52      /**
53       * Logger.
54       */
55      private static final Log LOG = LogFactory.getLog(ManageMethodsLocalUIHandler.class);
56  
57      /** {@inheritDoc} */
58      @Override
59      public void beforeInit(final ManageMethodsLocalUI ui) {
60          super.beforeInit(ui);
61  
62          // create model and register to the JAXX context
63          final ManageMethodsLocalUIModel model = new ManageMethodsLocalUIModel();
64          ui.setContextValue(model);
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      public void afterInit(final ManageMethodsLocalUI ui) {
70          initUI(ui);
71  
72          // Initialisation du tableau
73          initTable();
74  
75          ManageMethodsMenuUIModel menuUIModel = getUI().getMenuUI().getModel();
76          menuUIModel.setLocal(true);
77  
78          menuUIModel.addPropertyChangeListener(ManageMethodsMenuUIModel.PROPERTY_RESULTS, evt -> loadMethods((List<MethodDTO>) evt.getNewValue()));
79      }
80  
81      private void loadMethods(List<MethodDTO> methods) {
82  
83          getUI().getManageMethodsLocalTableNouveauBouton().setEnabled(true);
84  
85          getModel().setBeans(methods);
86          getModel().setModify(false);
87      }
88  
89      /**
90       * Initialisation du tableau.
91       */
92      private void initTable() {
93  
94          // name
95          final TableColumnExt mnemonicCol = addColumn(MethodsTableModel.NAME);
96          mnemonicCol.setSortable(true);
97          mnemonicCol.setEditable(true);
98  
99          // description
100         final TableColumnExt descriptionCol = addColumn(MethodsTableModel.DESCRIPTION);
101         descriptionCol.setSortable(true);
102         descriptionCol.setEditable(true);
103 
104         // reference
105         final TableColumnExt referenceCol = addColumn(MethodsTableModel.REFERENCE);
106         referenceCol.setSortable(true);
107         referenceCol.setEditable(true);
108 
109         // Comment, creation and update dates
110         addCommentColumn(MethodsTableModel.COMMENT);
111         TableColumnExt creationDateCol = addDatePickerColumnToModel(MethodsTableModel.CREATION_DATE, getConfig().getDateTimeFormat(), false);
112         fixColumnWidth(creationDateCol, 120);
113         TableColumnExt updateDateCol = addDatePickerColumnToModel(MethodsTableModel.UPDATE_DATE, getConfig().getDateTimeFormat(), false);
114         fixColumnWidth(updateDateCol, 120);
115 
116         // status
117         final TableColumnExt statusCol = addFilterableComboDataColumnToModel(
118                 MethodsTableModel.STATUS,
119                 getContext().getReferentialService().getStatus(StatusFilter.LOCAL),
120                 false);
121         statusCol.setSortable(true);
122         statusCol.setEditable(true);
123         fixDefaultColumnWidth(statusCol);
124 
125         // number
126 //		final TableColumnExt numberCol = addColumn(columnModel, MethodsTableModel.NUMBER);
127 //		numberCol.setSortable(true);
128 //		numberCol.setEditable(true);
129 
130         // description packaging
131         final TableColumnExt descriptionPackagingCol = addColumn(MethodsTableModel.DESCRIPTIONPACKAGING);
132         descriptionPackagingCol.setSortable(true);
133         descriptionPackagingCol.setEditable(true);
134 
135         // description preparation
136         final TableColumnExt descriptionPreparationCol = addColumn(MethodsTableModel.DESCRIPTIONPREPARATION);
137         descriptionPreparationCol.setSortable(true);
138         descriptionPreparationCol.setEditable(true);
139 
140         // description
141         final TableColumnExt descriptionPreservationCol = addColumn(MethodsTableModel.DESCRIPTIONPRESERVATION);
142         descriptionPreservationCol.setSortable(true);
143         descriptionPreservationCol.setEditable(true);
144 
145         MethodsTableModel tableModel = new MethodsTableModel(getTable().getColumnModel(), true);
146         getTable().setModel(tableModel);
147 
148         // Add extraction action
149         addExportToCSVAction(t("reefdb.property.pmfm.methods.local"));
150 
151         // Initialisation du tableau
152         initTable(getTable());
153 
154         // Les colonnes optionnelles sont invisibles
155         descriptionPackagingCol.setVisible(false);
156         descriptionPreparationCol.setVisible(false);
157         descriptionPreservationCol.setVisible(false);
158 
159         creationDateCol.setVisible(false);
160         updateDateCol.setVisible(false);
161 
162         getTable().setVisibleRowCount(5);
163     }
164 
165     /** {@inheritDoc} */
166     @Override
167     public AbstractReefDbTableModel<MethodsTableRowModel> getTableModel() {
168         return (MethodsTableModel) getTable().getModel();
169     }
170 
171     /** {@inheritDoc} */
172     @Override
173     public SwingTable getTable() {
174         return ui.getManageMethodsLocalTable();
175     }
176 
177     /** {@inheritDoc} */
178     @Override
179     protected void onRowsAdded(List<MethodsTableRowModel> addedRows) {
180         super.onRowsAdded(addedRows);
181 
182         if (addedRows.size() == 1) {
183             MethodsTableRowModel rowModel = addedRows.get(0);
184             // Set default status
185             rowModel.setStatus(Daos.getStatus(StatusCode.LOCAL_ENABLE));
186             setFocusOnCell(rowModel);
187         }
188     }
189 
190     /** {@inheritDoc} */
191     @Override
192     protected void onRowModified(int rowIndex, MethodsTableRowModel row, String propertyName, Integer propertyIndex, Object oldValue, Object newValue) {
193         super.onRowModified(rowIndex, row, propertyName, propertyIndex, oldValue, newValue);
194         row.setDirty(true);
195         forceRevalidateModel();
196     }
197 
198     /** {@inheritDoc} */
199     @Override
200     protected boolean isRowValid(MethodsTableRowModel row) {
201         row.getErrors().clear();
202         return super.isRowValid(row) && isUnique(row);
203     }
204 
205     private boolean isUnique(MethodsTableRowModel row) {
206 
207         if (StringUtils.isNotBlank(row.getName())) {
208             boolean duplicateFound = false;
209 
210             // check unicity in local table
211             for (MethodsTableRowModel otherRow : getModel().getRows()) {
212                 if (row == otherRow) continue;
213                 if (row.getName().equalsIgnoreCase(otherRow.getName())) {
214                     // duplicate found in table
215                     ReefDbBeans.addError(row,
216                             t("reefdb.error.alreadyExists.referential", t("reefdb.property.pmfm.method"), row.getName(), t("reefdb.property.referential.local")),
217                             MethodsTableRowModel.PROPERTY_NAME);
218                     duplicateFound = true;
219                     break;
220                 }
221             }
222 
223             if (!duplicateFound) {
224                 // check unicity in database
225                 List<MethodDTO> existingMethods = getContext().getReferentialService().searchMethods(StatusFilter.ALL, null, null);
226                 if (CollectionUtils.isNotEmpty(existingMethods)) {
227                     for (MethodDTO method : existingMethods) {
228                         if (!method.getId().equals(row.getId())
229                                 && row.getName().equalsIgnoreCase(method.getName())) {
230                             // duplicate found in database
231                             ReefDbBeans.addError(row,
232                                     t("reefdb.error.alreadyExists.referential",
233                                             t("reefdb.property.pmfm.method"), row.getName(), ReefDbBeans.isLocalStatus(method.getStatus())
234                                                     ? t("reefdb.property.referential.local") : t("reefdb.property.referential.national")),
235                                     MethodsTableRowModel.PROPERTY_NAME);
236                             break;
237                         }
238                     }
239                 }
240             }
241         }
242 
243         return row.getErrors().isEmpty();
244 
245     }
246 
247 }