View Javadoc
1   /*
2    * To change this license header, choose License Headers in Project Properties.
3    * To change this template file, choose Tools | Templates
4    * and open the template in the editor.
5    */
6   
7   package fr.ifremer.reefdb.ui.swing.content.manage.referential.user.table;
8   
9   /*
10   * #%L
11   * Reef DB :: UI
12   * $Id:$
13   * $HeadURL:$
14   * %%
15   * Copyright (C) 2014 - 2015 Ifremer
16   * %%
17   * This program is free software: you can redistribute it and/or modify
18   * it under the terms of the GNU Affero General Public License as published by
19   * the Free Software Foundation, either version 3 of the License, or
20   * (at your option) any later version.
21   * 
22   * This program is distributed in the hope that it will be useful,
23   * but WITHOUT ANY WARRANTY; without even the implied warranty of
24   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25   * GNU General Public License for more details.
26   * 
27   * You should have received a copy of the GNU Affero General Public License
28   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29   * #L%
30   */
31  
32  import fr.ifremer.reefdb.dto.referential.DepartmentDTO;
33  import fr.ifremer.quadrige3.ui.core.dto.referential.StatusDTO;
34  import fr.ifremer.reefdb.service.StatusFilter;
35  import fr.ifremer.reefdb.ui.swing.util.table.AbstractReefDbTableModel;
36  import fr.ifremer.reefdb.ui.swing.util.table.ReefDbColumnIdentifier;
37  import org.apache.commons.lang3.StringUtils;
38  import org.jdesktop.swingx.table.TableColumnModelExt;
39  
40  import java.util.Date;
41  
42  import static org.nuiton.i18n.I18n.n;
43  
44  /**
45   * <p>UserTableModel class.</p>
46   *
47   * @author Lionel Touseau <lionel.touseau@e-is.pro>
48   */
49  public class UserTableModel extends AbstractReefDbTableModel<UserRowModel> {
50  
51      /**
52       * <p>Constructor for UserTableModel.</p>
53       *
54       * @param columnModel a {@link org.jdesktop.swingx.table.TableColumnModelExt} object.
55       * @param createNewRowAllowed a boolean.
56       */
57      public UserTableModel(final TableColumnModelExt columnModel, boolean createNewRowAllowed) {
58          super(columnModel, createNewRowAllowed, false);
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public UserRowModel createNewRow() {
64          return new UserRowModel();
65      }
66  
67      /** Constant <code>REG_CODE</code> */
68      public static final ReefDbColumnIdentifier<UserRowModel> REG_CODE = ReefDbColumnIdentifier.newId(
69              UserRowModel.PROPERTY_REG_CODE,
70              n("reefdb.property.user.regCode"),
71              n("reefdb.property.user.regCode"),
72              String.class);
73  
74      /** Constant <code>LASTNAME</code> */
75      public static final ReefDbColumnIdentifier<UserRowModel> LASTNAME = ReefDbColumnIdentifier.newId(
76              UserRowModel.PROPERTY_NAME,
77              n("reefdb.property.user.lastname"),
78              n("reefdb.property.user.lastname"),
79              String.class,
80              true);
81  
82      /** Constant <code>FIRSTNAME</code> */
83      public static final ReefDbColumnIdentifier<UserRowModel> FIRSTNAME = ReefDbColumnIdentifier.newId(
84              UserRowModel.PROPERTY_FIRST_NAME,
85              n("reefdb.property.user.firstname"),
86              n("reefdb.property.user.firstname"),
87              String.class,
88              true);
89  
90      /** Constant <code>DEPARTMENT</code> */
91      public static final ReefDbColumnIdentifier<UserRowModel> DEPARTMENT = ReefDbColumnIdentifier.newId(
92              UserRowModel.PROPERTY_DEPARTMENT,
93              n("reefdb.property.department"),
94              n("reefdb.property.department"),
95              DepartmentDTO.class,
96              true);
97  
98      /** Constant <code>INTRANET_LOGIN</code> */
99      public static final ReefDbColumnIdentifier<UserRowModel> INTRANET_LOGIN = ReefDbColumnIdentifier.newId(
100             UserRowModel.PROPERTY_INTRANET_LOGIN,
101             n("reefdb.property.user.intranetLogin"),
102             n("reefdb.property.user.intranetLogin"),
103             String.class,
104             true);
105 
106     /** Constant <code>EXTRANET_LOGIN</code> */
107     public static final ReefDbColumnIdentifier<UserRowModel> EXTRANET_LOGIN = ReefDbColumnIdentifier.newId(
108             UserRowModel.PROPERTY_EXTRANET_LOGIN,
109             n("reefdb.property.user.extranetLogin"),
110             n("reefdb.property.user.extranetLogin"),
111             String.class);
112 
113     /** Constant <code>HAS_PASSWORD</code> */
114     public static final ReefDbColumnIdentifier<UserRowModel> HAS_PASSWORD = ReefDbColumnIdentifier.newId(
115             UserRowModel.PROPERTY_HAS_PASSWORD,
116             n("reefdb.property.user.password"),
117             n("reefdb.property.user.password"),
118             Boolean.class);
119 
120     /** Constant <code>EMAIL</code> */
121     public static final ReefDbColumnIdentifier<UserRowModel> EMAIL = ReefDbColumnIdentifier.newId(
122             UserRowModel.PROPERTY_EMAIL,
123             n("reefdb.property.email"),
124             n("reefdb.property.email"),
125             String.class);
126 
127     /** Constant <code>PHONE</code> */
128     public static final ReefDbColumnIdentifier<UserRowModel> PHONE = ReefDbColumnIdentifier.newId(
129             UserRowModel.PROPERTY_PHONE,
130             n("reefdb.property.phone"),
131             n("reefdb.property.phone"),
132             String.class);
133 
134     /** Constant <code>ADDRESS</code> */
135     public static final ReefDbColumnIdentifier<UserRowModel> ADDRESS = ReefDbColumnIdentifier.newId(
136             UserRowModel.PROPERTY_ADDRESS,
137             n("reefdb.property.address"),
138             n("reefdb.property.address"),
139             String.class);
140 
141     /** Constant <code>STATUS</code> */
142     public static final ReefDbColumnIdentifier<UserRowModel> STATUS = ReefDbColumnIdentifier.newId(
143             UserRowModel.PROPERTY_STATUS,
144             n("reefdb.property.status"),
145             n("reefdb.property.status"),
146             StatusDTO.class,
147             true);
148 
149     // PRIVILEGES
150     /** Constant <code>PRIVILEGES</code> */
151     public static final ReefDbColumnIdentifier<UserRowModel> PRIVILEGES = ReefDbColumnIdentifier.newId(
152             UserRowModel.PROPERTY_PRIVILEGE_SIZE,
153             n("reefdb.property.user.privileges"),
154             n("reefdb.property.user.privileges"),
155             Integer.class);
156 
157     /** Constant <code>ORGANISM</code> */
158     public static final ReefDbColumnIdentifier<UserRowModel> ORGANISM = ReefDbColumnIdentifier.newId(
159             UserRowModel.PROPERTY_ORGANISM,
160             n("reefdb.property.department"),
161             n("reefdb.property.department"),
162             String.class);
163 
164     /** Constant <code>ADMIN_CENTER</code> */
165     public static final ReefDbColumnIdentifier<UserRowModel> ADMIN_CENTER = ReefDbColumnIdentifier.newId(
166             UserRowModel.PROPERTY_ADMIN_CENTER,
167             n("reefdb.property.user.adminCenter"),
168             n("reefdb.property.user.adminCenter"),
169             String.class);
170 
171     /** Constant <code>SITE</code> */
172     public static final ReefDbColumnIdentifier<UserRowModel> SITE = ReefDbColumnIdentifier.newId(
173             UserRowModel.PROPERTY_SITE,
174             n("reefdb.property.user.site"),
175             n("reefdb.property.user.site"),
176             String.class);
177 
178 
179     public static final ReefDbColumnIdentifier<UserRowModel> COMMENT = ReefDbColumnIdentifier.newId(
180         UserRowModel.PROPERTY_COMMENT,
181         n("reefdb.property.comment"),
182         n("reefdb.property.comment"),
183         String.class,
184         false);
185 
186     public static final ReefDbColumnIdentifier<UserRowModel> CREATION_DATE = ReefDbColumnIdentifier.newReadOnlyId(
187         UserRowModel.PROPERTY_CREATION_DATE,
188         n("reefdb.property.date.creation"),
189         n("reefdb.property.date.creation"),
190         Date.class);
191 
192     public static final ReefDbColumnIdentifier<UserRowModel> UPDATE_DATE = ReefDbColumnIdentifier.newReadOnlyId(
193         UserRowModel.PROPERTY_UPDATE_DATE,
194         n("reefdb.property.date.modification"),
195         n("reefdb.property.date.modification"),
196         Date.class);
197 
198 
199 
200     /** {@inheritDoc} */
201     @Override
202     public ReefDbColumnIdentifier<UserRowModel> getFirstColumnEditing() {
203         return LASTNAME;
204     }
205 
206     /** {@inheritDoc} */
207     @Override
208     public boolean isCellEditable(int rowIndex, int columnIndex, org.nuiton.jaxx.application.swing.table.ColumnIdentifier<UserRowModel> propertyName) {
209 
210         boolean editable = super.isCellEditable(rowIndex, columnIndex, propertyName);
211 
212         if (editable) {
213             if (propertyName == PRIVILEGES) {
214 
215                 // local privileges can be empty BUT editable
216                 UserRowModel row = getEntry(rowIndex);
217                 editable = row.getStatus() == null
218                         || !StatusFilter.NATIONAL.toStatusCodes().contains(row.getStatus().getCode())
219                         || row.sizePrivilege() > 0;
220 
221             } else if (propertyName == HAS_PASSWORD) {
222 
223                 // password changes is allowed for admins and for the user itself
224                 UserRowModel row = getEntry(rowIndex);
225                 editable = (getTableUIModel().isAdmin() || getTableUIModel().isUserItself(row)) && StringUtils.isNotBlank(row.getIntranetLogin());
226             }
227 
228         }
229 
230         return editable;
231 
232     }
233 
234     /** {@inheritDoc} */
235     @Override
236     public UserTableUIModel getTableUIModel() {
237         return (UserTableUIModel) super.getTableUIModel();
238     }
239 }