]> git.mxchange.org Git - jfinancials-war.git/blob
7f0311ce564b263e98e62b21e125f2a679fc8aab
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017, 2020 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jfinancials.beans.business.department;
18
19 import java.util.List;
20 import javax.ejb.EJB;
21 import javax.enterprise.context.RequestScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.inject.Any;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import org.mxchange.jcontactsbusiness.events.department.added.DepartmentAddedEvent;
27 import org.mxchange.jcontactsbusiness.events.department.added.ObservableDepartmentAddedEvent;
28 import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentAlreadyAddedException;
29 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
30 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
31 import org.mxchange.jcontactsbusiness.model.department.AdminDepartmentSessionBeanRemote;
32 import org.mxchange.jcontactsbusiness.model.department.BusinessDepartment;
33 import org.mxchange.jcontactsbusiness.model.department.Department;
34 import org.mxchange.jcontactsbusiness.model.department.Departments;
35 import org.mxchange.jcontactsbusiness.model.employee.Employable;
36 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
37 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
38 import org.mxchange.jusercore.model.user.User;
39
40 /**
41  * An administrative bean for departments
42  * <p>
43  * @author Roland Häder<roland@mxchange.org>
44  */
45 @Named ("adminDepartmentController")
46 @RequestScoped
47 public class FinancialsAdminDepartmentWebRequestBean extends BaseFinancialsBean implements FinancialsAdminDepartmentWebRequestController {
48
49         /**
50          * Serial number
51          */
52         private static final long serialVersionUID = 5_028_697_360_464L;
53
54         /**
55          * EJB for administrative purposes
56          */
57         @EJB (lookup = "java:global/jfinancials-ejb/adminDepartment!org.mxchange.jcontactsbusiness.model.department.AdminDepartmentSessionBeanRemote")
58         private AdminDepartmentSessionBeanRemote adminDepartmentBean;
59
60         /**
61          * An event being fired when a department has been successfully added
62          */
63         @Inject
64         @Any
65         private Event<ObservableDepartmentAddedEvent> departmentAddedEvent;
66
67         /**
68          * Assigned branch office (if apply-able)
69          */
70         private BranchOffice departmentBranchOffice;
71
72         /**
73          * Assigned company for this department
74          */
75         private BasicData departmentCompany;
76
77         /**
78          * A general department controller (backing bean)
79          */
80         @Inject
81         private FinancialsDepartmentWebRequestController departmentController;
82
83         /**
84          * Assigned headquarter (if apply-able)
85          */
86         private Headquarter departmentHeadquarter;
87
88         /**
89          * Department name
90          */
91         private String departmentI18nKey;
92
93         /**
94          * Lead person of this department
95          */
96         private Employable departmentLead;
97
98         /**
99          * Owning user instance (which this department is assigned to)
100          */
101         private User departmentUserOwner;
102
103         /**
104          * Default constructor
105          */
106         public FinancialsAdminDepartmentWebRequestBean () {
107                 // Call super constructor
108                 super();
109         }
110
111         /**
112          * Adds department with all data from this backing bean. First this action
113          * method will validate if the department's address is already registered
114          * and if found, it will output a proper faces message.
115          */
116         public void addDepartment () {
117                 // Get instance
118                 final Department department = this.createDepartment();
119
120                 // Is the department not created yet?
121                 if (this.isDepartmentCreatedByRequiredData(department)) {
122                         // Then show proper faces message
123                         this.showFacesMessage("form-admin-add-department:branchStreet", "ADMIN_DEPARTMENT_ALREADY_CREATED"); //NOI18N
124                         return;
125                 }
126
127                 // Delcare updated instance
128                 final Department updatedDepartment;
129
130                 try {
131                         // Try to call EJB
132                         updatedDepartment = this.adminDepartmentBean.addDepartment(department);
133                 } catch (final DepartmentAlreadyAddedException ex) {
134                         // Output message
135                         this.showFacesMessage("form-admin-add-department:departmentI18nKey", "ADMIN_DEPARTMENT_ALREADY_CREATED"); //NOI18N
136                         return;
137                 }
138
139                 // Fire event
140                 this.departmentAddedEvent.fire(new DepartmentAddedEvent(updatedDepartment));
141         }
142
143         /**
144          * Getter for assigned branch office
145          * <p>
146          * @return Branch office
147          */
148         public BranchOffice getDepartmentBranchOffice () {
149                 return this.departmentBranchOffice;
150         }
151
152         /**
153          * Setter for assigned branch office
154          * <p>
155          * @param departmentBranchOffice Branch office
156          */
157         public void setDepartmentBranchOffice (final BranchOffice departmentBranchOffice) {
158                 this.departmentBranchOffice = departmentBranchOffice;
159         }
160
161         /**
162          * Getter for basic company data
163          * <p>
164          * @return Basic company data
165          */
166         public BasicData getDepartmentCompany () {
167                 return this.departmentCompany;
168         }
169
170         /**
171          * Setter for basic company data
172          * <p>
173          * @param departmentCompany Basic company data
174          */
175         public void setDepartmentCompany (final BasicData departmentCompany) {
176                 this.departmentCompany = departmentCompany;
177         }
178
179         /**
180          * Getter for assigned headquarter data
181          * <p>
182          * @return Headquarter data
183          */
184         public Headquarter getDepartmentHeadquarter () {
185                 return this.departmentHeadquarter;
186         }
187
188         /**
189          * Setter for assigned headquarter data
190          * <p>
191          * @param departmentHeadquarter Headquarter data
192          */
193         public void setDepartmentHeadquarter (final Headquarter departmentHeadquarter) {
194                 this.departmentHeadquarter = departmentHeadquarter;
195         }
196
197         /**
198          * Getter for department name
199          * <p>
200          * @return Department name
201          */
202         public String getDepartmentI18nKey () {
203                 return this.departmentI18nKey;
204         }
205
206         /**
207          * Setter for department name
208          * <p>
209          * @param departmentI18nKey Department name
210          */
211         public void setDepartmentI18nKey (final String departmentI18nKey) {
212                 this.departmentI18nKey = departmentI18nKey;
213         }
214
215         /**
216          * Getter for department contact person
217          * <p>
218          * @return Department contact person
219          */
220         public Employable getDepartmentLead () {
221                 return this.departmentLead;
222         }
223
224         /**
225          * Setter for department contact person
226          * <p>
227          * @param departmentLead Department contact person
228          */
229         public void setDepartmentLead (final Employable departmentLead) {
230                 this.departmentLead = departmentLead;
231         }
232
233         /**
234          * Getter for owning user instance
235          * <p>
236          * @return Owning user instance
237          */
238         public User getDepartmentUserOwner () {
239                 return this.departmentUserOwner;
240         }
241
242         /**
243          * Setter for owning user instance
244          * <p>
245          * @param departmentUserOwner Owning user instance
246          */
247         public void setDepartmentUserOwner (final User departmentUserOwner) {
248                 this.departmentUserOwner = departmentUserOwner;
249         }
250
251         /**
252          * Prepares an instance of a Department object (entity) with all data from
253          * this bean. If a complete fax number or land-line number was provided, it
254          * will be set in the instance as well.
255          * <p>
256          * @return An instance of a Department class (entity)
257          */
258         private Department createDepartment () {
259                 // Create new department instance
260                 final Department department = new BusinessDepartment(this.getDepartmentCompany(), this.getDepartmentI18nKey());
261
262                 // Add all optional fields
263                 department.setDepartmentHeadquarter(this.getDepartmentHeadquarter());
264                 department.setDepartmentBranchOffice(this.getDepartmentBranchOffice());
265                 department.setDepartmentLead(this.getDepartmentLead());
266                 department.setDepartmentUserOwner(this.getDepartmentUserOwner());
267
268                 // Return fully prepared instance
269                 return department;
270         }
271
272         /**
273          * Checks whether the given department is already found in local cache.
274          * Please note that this method fully relies on the cache, so you must
275          * always fire proper events that add/update/delete entries in cache.
276          * <p>
277          * @param department Department to check it's address
278          * <p>
279          * @return Whether the address has been found
280          */
281         private boolean isDepartmentCreatedByRequiredData (final Department department) {
282                 // Get full list from other bean
283                 final List<Department> departments = this.departmentController.allDepartments();
284
285                 // Default is not found
286                 boolean isFound = false;
287
288                 // Now check each entry
289                 for (final Department dep : departments) {
290                         // Is same address?
291                         if (Departments.isSameDepartment(dep, department)) {
292                                 // Found one
293                                 isFound = true;
294                                 break;
295                         }
296                 }
297
298                 // Return flag
299                 return isFound;
300         }
301
302 }