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.dali.ui.swing.content.manage.referential.user.table;
8   
9   /*
10   * #%L
11   * Dali :: 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.dali.dto.referential.DepartmentDTO;
33  import fr.ifremer.dali.service.StatusFilter;
34  import fr.ifremer.dali.ui.swing.util.table.AbstractDaliTableModel;
35  import fr.ifremer.dali.ui.swing.util.table.DaliColumnIdentifier;
36  import fr.ifremer.quadrige3.ui.core.dto.referential.StatusDTO;
37  import fr.ifremer.quadrige3.ui.swing.table.SwingTableColumnModel;
38  import org.apache.commons.lang3.StringUtils;
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 AbstractDaliTableModel<UserRowModel> {
50  
51      /**
52       * <p>Constructor for UserTableModel.</p>
53       *
54       * @param createNewRowAllowed a boolean.
55       */
56      public UserTableModel(final SwingTableColumnModel columnModel, boolean createNewRowAllowed) {
57          super(columnModel, createNewRowAllowed, false);
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public UserRowModel createNewRow() {
63          return new UserRowModel();
64      }
65  
66      /** Constant <code>REG_CODE</code> */
67      public static final DaliColumnIdentifier<UserRowModel> REG_CODE = DaliColumnIdentifier.newId(
68              UserRowModel.PROPERTY_REG_CODE,
69              n("dali.property.user.regCode"),
70              n("dali.property.user.regCode"),
71              String.class);
72  
73      /** Constant <code>LASTNAME</code> */
74      public static final DaliColumnIdentifier<UserRowModel> LASTNAME = DaliColumnIdentifier.newId(
75              UserRowModel.PROPERTY_NAME,
76              n("dali.property.user.lastname"),
77              n("dali.property.user.lastname"),
78              String.class,
79              true);
80  
81      /** Constant <code>FIRSTNAME</code> */
82      public static final DaliColumnIdentifier<UserRowModel> FIRSTNAME = DaliColumnIdentifier.newId(
83              UserRowModel.PROPERTY_FIRST_NAME,
84              n("dali.property.user.firstname"),
85              n("dali.property.user.firstname"),
86              String.class,
87              true);
88  
89      /** Constant <code>DEPARTMENT</code> */
90      public static final DaliColumnIdentifier<UserRowModel> DEPARTMENT = DaliColumnIdentifier.newId(
91              UserRowModel.PROPERTY_DEPARTMENT,
92              n("dali.property.department"),
93              n("dali.property.department"),
94              DepartmentDTO.class,
95              true);
96  
97      /** Constant <code>INTRANET_LOGIN</code> */
98      public static final DaliColumnIdentifier<UserRowModel> INTRANET_LOGIN = DaliColumnIdentifier.newId(
99              UserRowModel.PROPERTY_INTRANET_LOGIN,
100             n("dali.property.user.intranetLogin"),
101             n("dali.property.user.intranetLogin"),
102             String.class,
103             true);
104 
105     /** Constant <code>EXTRANET_LOGIN</code> */
106     public static final DaliColumnIdentifier<UserRowModel> EXTRANET_LOGIN = DaliColumnIdentifier.newId(
107             UserRowModel.PROPERTY_EXTRANET_LOGIN,
108             n("dali.property.user.extranetLogin"),
109             n("dali.property.user.extranetLogin"),
110             String.class);
111 
112     /** Constant <code>HAS_PASSWORD</code> */
113     public static final DaliColumnIdentifier<UserRowModel> HAS_PASSWORD = DaliColumnIdentifier.newId(
114             UserRowModel.PROPERTY_HAS_PASSWORD,
115             n("dali.property.user.password"),
116             n("dali.property.user.password"),
117             Boolean.class);
118 
119     /** Constant <code>EMAIL</code> */
120     public static final DaliColumnIdentifier<UserRowModel> EMAIL = DaliColumnIdentifier.newId(
121             UserRowModel.PROPERTY_EMAIL,
122             n("dali.property.email"),
123             n("dali.property.email"),
124             String.class);
125 
126     /** Constant <code>PHONE</code> */
127     public static final DaliColumnIdentifier<UserRowModel> PHONE = DaliColumnIdentifier.newId(
128             UserRowModel.PROPERTY_PHONE,
129             n("dali.property.phone"),
130             n("dali.property.phone"),
131             String.class);
132 
133     /** Constant <code>ADDRESS</code> */
134     public static final DaliColumnIdentifier<UserRowModel> ADDRESS = DaliColumnIdentifier.newId(
135             UserRowModel.PROPERTY_ADDRESS,
136             n("dali.property.address"),
137             n("dali.property.address"),
138             String.class);
139 
140     /** Constant <code>STATUS</code> */
141     public static final DaliColumnIdentifier<UserRowModel> STATUS = DaliColumnIdentifier.newId(
142             UserRowModel.PROPERTY_STATUS,
143             n("dali.property.status"),
144             n("dali.property.status"),
145             StatusDTO.class,
146             true);
147 
148     // PRIVILEGES
149     /** Constant <code>PRIVILEGES</code> */
150     public static final DaliColumnIdentifier<UserRowModel> PRIVILEGES = DaliColumnIdentifier.newId(
151             UserRowModel.PROPERTY_PRIVILEGE_SIZE,
152             n("dali.property.user.privileges"),
153             n("dali.property.user.privileges"),
154             Integer.class);
155 
156     /** Constant <code>ORGANISM</code> */
157     public static final DaliColumnIdentifier<UserRowModel> ORGANISM = DaliColumnIdentifier.newId(
158             UserRowModel.PROPERTY_ORGANISM,
159             n("dali.property.department"),
160             n("dali.property.department"),
161             String.class);
162 
163     /** Constant <code>ADMIN_CENTER</code> */
164     public static final DaliColumnIdentifier<UserRowModel> ADMIN_CENTER = DaliColumnIdentifier.newId(
165             UserRowModel.PROPERTY_ADMIN_CENTER,
166             n("dali.property.user.adminCenter"),
167             n("dali.property.user.adminCenter"),
168             String.class);
169 
170     /** Constant <code>SITE</code> */
171     public static final DaliColumnIdentifier<UserRowModel> SITE = DaliColumnIdentifier.newId(
172             UserRowModel.PROPERTY_SITE,
173             n("dali.property.user.site"),
174             n("dali.property.user.site"),
175             String.class);
176 
177     public static final DaliColumnIdentifier<UserRowModel> COMMENT = DaliColumnIdentifier.newId(
178         UserRowModel.PROPERTY_COMMENT,
179         n("dali.property.comment"),
180         n("dali.property.comment"),
181         String.class,
182         false);
183 
184     public static final DaliColumnIdentifier<UserRowModel> CREATION_DATE = DaliColumnIdentifier.newReadOnlyId(
185         UserRowModel.PROPERTY_CREATION_DATE,
186         n("dali.property.date.creation"),
187         n("dali.property.date.creation"),
188         Date.class);
189 
190     public static final DaliColumnIdentifier<UserRowModel> UPDATE_DATE = DaliColumnIdentifier.newReadOnlyId(
191         UserRowModel.PROPERTY_UPDATE_DATE,
192         n("dali.property.date.modification"),
193         n("dali.property.date.modification"),
194         Date.class);
195 
196 
197     /** {@inheritDoc} */
198     @Override
199     public DaliColumnIdentifier<UserRowModel> getFirstColumnEditing() {
200         return LASTNAME;
201     }
202 
203     /** {@inheritDoc} */
204     @Override
205     public boolean isCellEditable(int rowIndex, int columnIndex, org.nuiton.jaxx.application.swing.table.ColumnIdentifier<UserRowModel> propertyName) {
206 
207         boolean editable = super.isCellEditable(rowIndex, columnIndex, propertyName);
208 
209         if (editable) {
210             if (propertyName == PRIVILEGES) {
211 
212                 // local privileges can be empty BUT editable
213                 UserRowModel row = getEntry(rowIndex);
214                 editable = row.getStatus() == null
215                         || !StatusFilter.ALL.toStatusCodes().contains(row.getStatus().getCode())
216                         || row.sizePrivilege() > 0;
217 
218             } else if (propertyName == HAS_PASSWORD) {
219 
220                 // password changes is allowed for admins and for the user itself
221                 UserRowModel row = getEntry(rowIndex);
222                 editable = (getTableUIModel().isAdmin() || getTableUIModel().isUserItself(row)) && StringUtils.isNotBlank(row.getIntranetLogin());
223             }
224 
225         }
226 
227         return editable;
228 
229     }
230 
231     /** {@inheritDoc} */
232     @Override
233     public UserTableUIModel getTableUIModel() {
234         return (UserTableUIModel) super.getTableUIModel();
235     }
236 }