View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.referential.samplingequipment.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.SamplingEquipmentDTO;
31  import fr.ifremer.reefdb.service.StatusFilter;
32  import fr.ifremer.reefdb.ui.swing.content.manage.referential.samplingequipment.menu.SamplingEquipmentsMenuUIModel;
33  import fr.ifremer.reefdb.ui.swing.content.manage.referential.samplingequipment.table.SamplingEquipmentsTableModel;
34  import fr.ifremer.reefdb.ui.swing.content.manage.referential.samplingequipment.table.SamplingEquipmentsTableRowModel;
35  import fr.ifremer.reefdb.ui.swing.util.ReefDbUI;
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.List;
45  
46  import static org.nuiton.i18n.I18n.t;
47  
48  /**
49   * Controlleur pour l administration des enginsPrelevement au niveau local
50   */
51  public class SamplingEquipmentsLocalUIHandler extends
52          AbstractReefDbTableUIHandler<SamplingEquipmentsTableRowModel, SamplingEquipmentsLocalUIModel, SamplingEquipmentsLocalUI> {
53  
54      /**
55       * Logger.
56       */
57      private static final Log LOG = LogFactory.getLog(SamplingEquipmentsLocalUIHandler.class);
58  
59      /** {@inheritDoc} */
60      @Override
61      public void beforeInit(final SamplingEquipmentsLocalUI ui) {
62          super.beforeInit(ui);
63  
64          // create model and register to the JAXX context
65          final SamplingEquipmentsLocalUIModel model = new SamplingEquipmentsLocalUIModel();
66          ui.setContextValue(model);
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public void afterInit(final SamplingEquipmentsLocalUI ui) {
72          initUI(ui);
73  
74          // hide context filter panel
75          ui.getSamplingEquipmentsLocalMenuUI().getHandler().enableContextFilter(false);
76  
77          // force local
78          ui.getSamplingEquipmentsLocalMenuUI().getHandler().forceLocal(true);
79  
80          // init listener on found analysis instruments
81          ui.getSamplingEquipmentsLocalMenuUI().getModel().addPropertyChangeListener(SamplingEquipmentsMenuUIModel.PROPERTY_RESULTS, evt -> loadTable((List<SamplingEquipmentDTO>) evt.getNewValue()));
82  
83          // Initialisation du tableau
84          initTable();
85  
86          getUI().getReferentialSamplingEquipmentsLocalTableDeleteBouton().setEnabled(false);
87          getUI().getReferentialSamplingEquipmentsLocalTableReplaceBouton().setEnabled(false);
88  
89      }
90  
91      /**
92       * Initialisation du tableau.
93       */
94      private void initTable() {
95  
96          // mnemonic
97          final TableColumnExt mnemonicCol = addColumn(
98                  SamplingEquipmentsTableModel.NAME);
99          mnemonicCol.setSortable(true);
100 
101         // description
102         final TableColumnExt descriptionCol = addColumn(
103                 SamplingEquipmentsTableModel.DESCRIPTION);
104         descriptionCol.setSortable(true);
105 
106         // size
107         final TableColumnExt sizeCol = addColumn(
108                 newNumberCellEditor(Double.class, false, ReefDbUI.SIGNED_HIGH_DECIMAL_DIGITS_PATTERN),
109                 newNumberCellRenderer(10),
110                 SamplingEquipmentsTableModel.SIZE);
111         sizeCol.setSortable(true);
112 
113         // unit
114         final TableColumnExt unitCol = addFilterableComboDataColumnToModel(
115                 SamplingEquipmentsTableModel.UNIT,
116                 getContext().getReferentialService().getUnits(StatusFilter.ACTIVE),
117                 false);
118         unitCol.setSortable(true);
119 
120 
121         // Comment, creation and update dates
122         addCommentColumn(SamplingEquipmentsTableModel.COMMENT);
123         TableColumnExt creationDateCol = addDatePickerColumnToModel(SamplingEquipmentsTableModel.CREATION_DATE, getConfig().getDateTimeFormat(), false);
124         fixColumnWidth(creationDateCol, 120);
125         TableColumnExt updateDateCol = addDatePickerColumnToModel(SamplingEquipmentsTableModel.UPDATE_DATE, getConfig().getDateTimeFormat(), false);
126         fixColumnWidth(updateDateCol, 120);
127 
128         // status
129         final TableColumnExt statusCol = addFilterableComboDataColumnToModel(
130                 SamplingEquipmentsTableModel.STATUS,
131                 getContext().getReferentialService().getStatus(StatusFilter.LOCAL),
132                 false);
133         statusCol.setSortable(true);
134 
135         final SamplingEquipmentsTableModel tableModel = new SamplingEquipmentsTableModel(getTable().getColumnModel(), true);
136         getTable().setModel(tableModel);
137 
138         // Add extraction action
139         addExportToCSVAction(t("reefdb.property.samplingEquipments.local"));
140 
141         // Initialisation du tableau
142         initTable(getTable());
143 
144         creationDateCol.setVisible(false);
145         updateDateCol.setVisible(false);
146 
147         getTable().setVisibleRowCount(5);
148     }
149 
150     /**
151      * <p>clearTable.</p>
152      */
153     public void clearTable() {
154         getModel().setBeans(null);
155     }
156 
157     /**
158      * <p>loadTable.</p>
159      *
160      * @param samplingEquipments a {@link java.util.List} object.
161      */
162     public void loadTable(List<SamplingEquipmentDTO> samplingEquipments) {
163         getModel().setBeans(samplingEquipments);
164     }
165 
166     /** {@inheritDoc} */
167     @Override
168     public AbstractReefDbTableModel<SamplingEquipmentsTableRowModel> getTableModel() {
169         return (SamplingEquipmentsTableModel) getTable().getModel();
170     }
171 
172     /** {@inheritDoc} */
173     @Override
174     public SwingTable getTable() {
175         return getUI().getReferentialSamplingEquipmentsLocalTable();
176     }
177 
178     /** {@inheritDoc} */
179     @Override
180     protected void onRowsAdded(List<SamplingEquipmentsTableRowModel> addedRows) {
181         super.onRowsAdded(addedRows);
182 
183         if (addedRows.size() == 1) {
184             SamplingEquipmentsTableRowModel rowModel = addedRows.get(0);
185 
186             // Set default status
187             rowModel.setStatus(Daos.getStatus(StatusCode.LOCAL_ENABLE));
188 
189             setFocusOnCell(rowModel);
190         }
191 
192     }
193 
194     /** {@inheritDoc} */
195     @Override
196     protected void onRowModified(int rowIndex, SamplingEquipmentsTableRowModel row, String propertyName, Integer propertyIndex, Object oldValue, Object newValue) {
197         super.onRowModified(rowIndex, row, propertyName, propertyIndex, oldValue, newValue);
198         row.setDirty(true);
199         forceRevalidateModel();
200     }
201 
202     /** {@inheritDoc} */
203     @Override
204     protected boolean isRowValid(SamplingEquipmentsTableRowModel row) {
205         row.getErrors().clear();
206         return !row.isEditable() || super.isRowValid(row) && isSamplingEquipmentValid(row);
207     }
208 
209     private boolean isSamplingEquipmentValid(SamplingEquipmentsTableRowModel row) {
210 
211         // check unit not null if size not null
212         if (row.getSize() != null && row.getUnit() == null) {
213             ReefDbBeans.addError(row, t("reefdb.validator.error.field.empty"), SamplingEquipmentsTableRowModel.PROPERTY_UNIT);
214         }
215 
216         if (StringUtils.isNotBlank(row.getName())) {
217             boolean duplicateFound = false;
218 
219             // check unicity in local table
220             for (SamplingEquipmentsTableRowModel otherRow : getModel().getRows()) {
221                 if (row == otherRow) continue;
222                 if (row.getName().equalsIgnoreCase(otherRow.getName())) {
223                     // duplicate found in table
224                     ReefDbBeans.addError(row,
225                             t("reefdb.error.alreadyExists.referential", t("reefdb.property.samplingEquipment"), row.getName(), t("reefdb.property.referential.local")),
226                             SamplingEquipmentsTableRowModel.PROPERTY_NAME);
227                     duplicateFound = true;
228                     break;
229                 }
230             }
231 
232             if (!duplicateFound) {
233                 // check unicity in database
234                 List<SamplingEquipmentDTO> existingSamplingEquipments = getContext().getReferentialService().searchSamplingEquipments(StatusFilter.ALL, row.getName(), null);
235                 if (CollectionUtils.isNotEmpty(existingSamplingEquipments)) {
236                     for (SamplingEquipmentDTO samplingEquipment : existingSamplingEquipments) {
237                         if (!samplingEquipment.getId().equals(row.getId())) {
238                             // duplicate found in database
239                             ReefDbBeans.addError(row,
240                                     t("reefdb.error.alreadyExists.referential",
241                                             t("reefdb.property.samplingEquipment"), row.getName(), ReefDbBeans.isLocalStatus(samplingEquipment.getStatus())
242                                                     ? t("reefdb.property.referential.local") : t("reefdb.property.referential.national")),
243                                     SamplingEquipmentsTableRowModel.PROPERTY_NAME);
244                             break;
245                         }
246                     }
247                 }
248             }
249         }
250 
251         return row.getErrors().isEmpty();
252     }
253 
254 }