]> git.mxchange.org Git - jfinancials-war.git/blob
acde45d914facebcacc35b3615cda10f1eb7aafb
[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 javax.ejb.EJB;
20 import javax.enterprise.context.RequestScoped;
21 import javax.enterprise.event.Event;
22 import javax.enterprise.inject.Any;
23 import javax.inject.Inject;
24 import javax.inject.Named;
25 import org.mxchange.jcontactsbusiness.events.department.added.DepartmentAddedEvent;
26 import org.mxchange.jcontactsbusiness.events.department.added.ObservableDepartmentAddedEvent;
27 import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentAlreadyAddedException;
28 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
29 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
30 import org.mxchange.jcontactsbusiness.model.department.AdminDepartmentSessionBeanRemote;
31 import org.mxchange.jcontactsbusiness.model.department.BusinessDepartment;
32 import org.mxchange.jcontactsbusiness.model.department.Department;
33 import org.mxchange.jcontactsbusiness.model.employee.Employable;
34 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
35 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
36 import org.mxchange.jusercore.model.user.User;
37 import org.mxchange.jfinancials.beans.business.department.list.FinancialsDepartmentListWebViewController;
38
39 /**
40  * An administrative bean for departments
41  * <p>
42  * @author Roland Häder<roland@mxchange.org>
43  */
44 @Named ("adminDepartmentController")
45 @RequestScoped
46 public class FinancialsAdminDepartmentWebRequestBean extends BaseFinancialsBean implements FinancialsAdminDepartmentWebRequestController {
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 5_028_697_360_464L;
52
53         /**
54          * EJB for administrative purposes
55          */
56         @EJB (lookup = "java:global/jfinancials-ejb/adminDepartment!org.mxchange.jcontactsbusiness.model.department.AdminDepartmentSessionBeanRemote")
57         private AdminDepartmentSessionBeanRemote adminDepartmentBean;
58
59         /**
60          * An event being fired when a department has been successfully added
61          */
62         @Inject
63         @Any
64         private Event<ObservableDepartmentAddedEvent> departmentAddedEvent;
65
66         /**
67          * Assigned branch office (if apply-able)
68          */
69         private BranchOffice departmentBranchOffice;
70
71         /**
72          * Assigned company for this department
73          */
74         private BasicData departmentCompany;
75
76         /**
77          * Assigned headquarter (if apply-able)
78          */
79         private Headquarter departmentHeadquarter;
80
81         /**
82          * Department name
83          */
84         private String departmentI18nKey;
85
86         /**
87          * Lead person of this department
88          */
89         private Employable departmentLead;
90
91         /**
92          * A general department controller (backing bean)
93          */
94         @Inject
95         private FinancialsDepartmentListWebViewController departmentListController;
96
97         /**
98          * Owning user instance (which this department is assigned to)
99          */
100         private User departmentUserOwner;
101
102         /**
103          * Default constructor
104          */
105         public FinancialsAdminDepartmentWebRequestBean () {
106                 // Call super constructor
107                 super();
108         }
109
110         /**
111          * Adds department with all data from this backing bean. First this action
112          * method will validate if the department's address is already registered
113          * and if found, it will output a proper faces message.
114          */
115         public void addDepartment () {
116                 // Get instance
117                 final Department department = this.createDepartment();
118
119                 // Is the department not created yet?
120                 if (this.departmentListController.isDepartmentAlreadyAdded(department)) {
121                         // Then show proper faces message
122                         this.showFacesMessage("form-admin-add-department:branchStreet", "ADMIN_DEPARTMENT_ALREADY_CREATED"); //NOI18N
123                         return;
124                 }
125
126                 // Delcare updated instance
127                 final Department updatedDepartment;
128
129                 try {
130                         // Try to call EJB
131                         updatedDepartment = this.adminDepartmentBean.addDepartment(department);
132                 } catch (final DepartmentAlreadyAddedException ex) {
133                         // Output message
134                         this.showFacesMessage("form-admin-add-department:departmentI18nKey", "ADMIN_DEPARTMENT_ALREADY_CREATED"); //NOI18N
135                         return;
136                 }
137
138                 // Fire event
139                 this.departmentAddedEvent.fire(new DepartmentAddedEvent(updatedDepartment));
140         }
141
142         /**
143          * Getter for assigned branch office
144          * <p>
145          * @return Branch office
146          */
147         public BranchOffice getDepartmentBranchOffice () {
148                 return this.departmentBranchOffice;
149         }
150
151         /**
152          * Setter for assigned branch office
153          * <p>
154          * @param departmentBranchOffice Branch office
155          */
156         public void setDepartmentBranchOffice (final BranchOffice departmentBranchOffice) {
157                 this.departmentBranchOffice = departmentBranchOffice;
158         }
159
160         /**
161          * Getter for basic company data
162          * <p>
163          * @return Basic company data
164          */
165         public BasicData getDepartmentCompany () {
166                 return this.departmentCompany;
167         }
168
169         /**
170          * Setter for basic company data
171          * <p>
172          * @param departmentCompany Basic company data
173          */
174         public void setDepartmentCompany (final BasicData departmentCompany) {
175                 this.departmentCompany = departmentCompany;
176         }
177
178         /**
179          * Getter for assigned headquarter data
180          * <p>
181          * @return Headquarter data
182          */
183         public Headquarter getDepartmentHeadquarter () {
184                 return this.departmentHeadquarter;
185         }
186
187         /**
188          * Setter for assigned headquarter data
189          * <p>
190          * @param departmentHeadquarter Headquarter data
191          */
192         public void setDepartmentHeadquarter (final Headquarter departmentHeadquarter) {
193                 this.departmentHeadquarter = departmentHeadquarter;
194         }
195
196         /**
197          * Getter for department name
198          * <p>
199          * @return Department name
200          */
201         public String getDepartmentI18nKey () {
202                 return this.departmentI18nKey;
203         }
204
205         /**
206          * Setter for department name
207          * <p>
208          * @param departmentI18nKey Department name
209          */
210         public void setDepartmentI18nKey (final String departmentI18nKey) {
211                 this.departmentI18nKey = departmentI18nKey;
212         }
213
214         /**
215          * Getter for department contact person
216          * <p>
217          * @return Department contact person
218          */
219         public Employable getDepartmentLead () {
220                 return this.departmentLead;
221         }
222
223         /**
224          * Setter for department contact person
225          * <p>
226          * @param departmentLead Department contact person
227          */
228         public void setDepartmentLead (final Employable departmentLead) {
229                 this.departmentLead = departmentLead;
230         }
231
232         /**
233          * Getter for owning user instance
234          * <p>
235          * @return Owning user instance
236          */
237         public User getDepartmentUserOwner () {
238                 return this.departmentUserOwner;
239         }
240
241         /**
242          * Setter for owning user instance
243          * <p>
244          * @param departmentUserOwner Owning user instance
245          */
246         public void setDepartmentUserOwner (final User departmentUserOwner) {
247                 this.departmentUserOwner = departmentUserOwner;
248         }
249
250         /**
251          * Prepares an instance of a Department object (entity) with all data from
252          * this bean. If a complete fax number or land-line number was provided, it
253          * will be set in the instance as well.
254          * <p>
255          * @return An instance of a Department class (entity)
256          */
257         private Department createDepartment () {
258                 // Create new department instance
259                 final Department department = new BusinessDepartment(this.getDepartmentCompany(), this.getDepartmentI18nKey());
260
261                 // Add all optional fields
262                 department.setDepartmentHeadquarter(this.getDepartmentHeadquarter());
263                 department.setDepartmentBranchOffice(this.getDepartmentBranchOffice());
264                 department.setDepartmentLead(this.getDepartmentLead());
265                 department.setDepartmentUserOwner(this.getDepartmentUserOwner());
266
267                 // Return fully prepared instance
268                 return department;
269         }
270
271 }