]> git.mxchange.org Git - jfinancials-war.git/blob
a71d89b55ae7946f1d4a99e5fe8f97b0a8058912
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017 Roland Häder
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.BusinessBasicData;
30 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
31 import org.mxchange.jcontactsbusiness.model.department.AdminDepartmentSessionBeanRemote;
32 import org.mxchange.jcontactsbusiness.model.department.CompanyDepartment;
33 import org.mxchange.jcontactsbusiness.model.department.Department;
34 import org.mxchange.jcontactsbusiness.model.department.Departments;
35 import org.mxchange.jcontactsbusiness.model.employee.Employee;
36 import org.mxchange.jcontactsbusiness.model.headquarters.HeadquartersData;
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_462L;
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 BusinessBasicData departmentCompany;
76
77         /**
78          * A general department controller (backing bean)
79          */
80         @Inject
81         private FinancialsDepartmentWebRequestController departmentController;
82
83         /**
84          * Assigned headquarters (if apply-able)
85          */
86         private HeadquartersData departmentHeadquarters;
87
88         /**
89          * Lead person of this department
90          */
91         private Employee departmentLead;
92
93         /**
94          * Department name
95          */
96         private String departmentName;
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          * <p>
116          * @return Redirect outcome
117          */
118         public String addDepartment () {
119                 // Get instance
120                 final Department department = this.createDepartment();
121
122                 // Is the department not created yet?
123                 if (this.isDepartmentCreatedByRequiredData(department)) {
124                         // Then show proper faces message
125                         this.showFacesMessage("form-admin-add-department:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
126                         return ""; //NOI18N
127                 }
128
129                 // Delcare updated instance
130                 final Department updatedDepartment;
131
132                 try {
133                         // Try to call EJB
134                         updatedDepartment = this.adminDepartmentBean.addDepartment(department);
135                 } catch (final DepartmentAlreadyAddedException ex) {
136                         // Output message
137                         this.showFacesMessage("form-admin-add-department:departmentName", "ADMIN_DEPARTMENT_ALREADY_CREATED"); //NOI18N
138                         return ""; //NOI18N
139                 }
140
141                 // Fire event
142                 this.departmentAddedEvent.fire(new DepartmentAddedEvent(updatedDepartment));
143
144                 // Redirect to list
145                 return "admin_list_department"; //NOI18N
146         }
147
148         /**
149          * Getter for assigned branch office
150          * <p>
151          * @return Branch office
152          */
153         public BranchOffice getDepartmentBranchOffice () {
154                 return this.departmentBranchOffice;
155         }
156
157         /**
158          * Setter for assigned branch office
159          * <p>
160          * @param departmentBranchOffice Branch office
161          */
162         public void setDepartmentBranchOffice (final BranchOffice departmentBranchOffice) {
163                 this.departmentBranchOffice = departmentBranchOffice;
164         }
165
166         /**
167          * Getter for basic company data
168          * <p>
169          * @return Basic company data
170          */
171         public BusinessBasicData getDepartmentCompany () {
172                 return this.departmentCompany;
173         }
174
175         /**
176          * Setter for basic company data
177          * <p>
178          * @param departmentCompany Basic company data
179          */
180         public void setDepartmentCompany (final BusinessBasicData departmentCompany) {
181                 this.departmentCompany = departmentCompany;
182         }
183
184         /**
185          * Getter for assigned headquarters data
186          * <p>
187          * @return Headquarters data
188          */
189         public HeadquartersData getDepartmentHeadquarters () {
190                 return this.departmentHeadquarters;
191         }
192
193         /**
194          * Setter for assigned headquarters data
195          * <p>
196          * @param departmentHeadquarters Headquarters data
197          */
198         public void setDepartmentHeadquarters (final HeadquartersData departmentHeadquarters) {
199                 this.departmentHeadquarters = departmentHeadquarters;
200         }
201
202         /**
203          * Getter for department contact person
204          * <p>
205          * @return Department contact person
206          */
207         public Employee getDepartmentLead () {
208                 return this.departmentLead;
209         }
210
211         /**
212          * Setter for department contact person
213          * <p>
214          * @param departmentLead Department contact person
215          */
216         public void setDepartmentLead (final Employee departmentLead) {
217                 this.departmentLead = departmentLead;
218         }
219
220         /**
221          * Getter for department name
222          * <p>
223          * @return Department name
224          */
225         public String getDepartmentName () {
226                 return this.departmentName;
227         }
228
229         /**
230          * Setter for department name
231          * <p>
232          * @param departmentName Department name
233          */
234         public void setDepartmentName (final String departmentName) {
235                 this.departmentName = departmentName;
236         }
237
238         /**
239          * Getter for owning user instance
240          * <p>
241          * @return Owning user instance
242          */
243         public User getDepartmentUserOwner () {
244                 return this.departmentUserOwner;
245         }
246
247         /**
248          * Setter for owning user instance
249          * <p>
250          * @param departmentUserOwner Owning user instance
251          */
252         public void setDepartmentUserOwner (final User departmentUserOwner) {
253                 this.departmentUserOwner = departmentUserOwner;
254         }
255
256         /**
257          * Prepares an instance of a Department object (entity) with all data from
258          * this bean. If a complete fax number or land-line number was provided, it
259          * will be set in the instance as well.
260          * <p>
261          * @return An instance of a Department class (entity)
262          */
263         private Department createDepartment () {
264                 // Create new department instance
265                 final Department department = new CompanyDepartment(this.getDepartmentCompany(), this.getDepartmentName());
266
267                 // Add all optional fields
268                 department.setDepartmentHeadquarters(this.getDepartmentHeadquarters());
269                 department.setDepartmentBranchOffice(this.getDepartmentBranchOffice());
270                 department.setDepartmentLead(this.getDepartmentLead());
271                 department.setDepartmentUserOwner(this.getDepartmentUserOwner());
272
273                 // Return fully prepared instance
274                 return department;
275         }
276
277         /**
278          * Checks whether the given department is already found in local cache.
279          * Please note that this method fully relies on the cache, so you must
280          * always fire proper events that add/update/delete entries in cache.
281          * <p>
282          * @param department Department to check it's address
283          * <p>
284          * @return Whether the address has been found
285          */
286         private boolean isDepartmentCreatedByRequiredData (final Department department) {
287                 // Get full list from other bean
288                 final List<Department> departments = this.departmentController.allDepartments();
289
290                 // Default is not found
291                 boolean isFound = false;
292
293                 // Now check each entry
294                 for (final Department dep : departments) {
295                         // Is same address?
296                         if (Departments.isSameDepartment(dep, department)) {
297                                 // Found one
298                                 isFound = true;
299                                 break;
300                         }
301                 }
302
303                 // Return flag
304                 return isFound;
305         }
306
307 }