]> git.mxchange.org Git - jfinancials-war.git/blob - src/java/org/mxchange/jfinancials/beans/business/department/action/FinancialsAdminDepartmentActionWebRequestBean.java
Updated copyright year
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / beans / business / department / action / FinancialsAdminDepartmentActionWebRequestBean.java
1 /*
2  * Copyright (C) 2017 - 2022 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.action;
18
19 import java.text.MessageFormat;
20 import java.util.Objects;
21 import javax.ejb.EJB;
22 import javax.enterprise.context.RequestScoped;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.inject.Any;
25 import javax.faces.FacesException;
26 import javax.faces.application.FacesMessage;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import org.mxchange.jcontacts.model.contact.Contact;
30 import org.mxchange.jcontactsbusiness.events.department.added.AdminDepartmentAddedEvent;
31 import org.mxchange.jcontactsbusiness.events.department.added.ObservableAdminDepartmentAddedEvent;
32 import org.mxchange.jcontactsbusiness.events.department.updated.AdminDepartmentUpdatedEvent;
33 import org.mxchange.jcontactsbusiness.events.department.updated.ObservableAdminDepartmentUpdatedEvent;
34 import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentAlreadyAddedException;
35 import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentNotFoundException;
36 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
37 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
38 import org.mxchange.jcontactsbusiness.model.department.AdminDepartmentSessionBeanRemote;
39 import org.mxchange.jcontactsbusiness.model.department.BusinessDepartment;
40 import org.mxchange.jcontactsbusiness.model.department.Department;
41 import org.mxchange.jcontactsbusiness.model.department.Departments;
42 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
43 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
44 import org.mxchange.jfinancials.beans.business.department.list.FinancialsDepartmentListWebViewController;
45 import org.mxchange.jusercore.model.user.User;
46
47 /**
48  * An administrative action bean for departments
49  * <p>
50  * @author Roland Häder<roland@mxchange.org>
51  */
52 @Named ("adminDepartmentActionController")
53 @RequestScoped
54 public class FinancialsAdminDepartmentActionWebRequestBean extends BaseFinancialsBean implements FinancialsAdminDepartmentActionWebRequestController {
55
56         /**
57          * Serial number
58          */
59         private static final long serialVersionUID = 5_028_697_360_473L;
60
61         /**
62          * EJB for administrative purposes
63          */
64         @EJB (lookup = "java:global/jfinancials-ejb/adminDepartment!org.mxchange.jcontactsbusiness.model.department.AdminDepartmentSessionBeanRemote")
65         private AdminDepartmentSessionBeanRemote adminDepartmentBean;
66
67         /**
68          * Currently worked on department
69          */
70         private Department currentDepartment;
71
72         /**
73          * An event being fired when a department has been successfully added
74          */
75         @Inject
76         @Any
77         private Event<ObservableAdminDepartmentAddedEvent> departmentAddedEvent;
78
79         /**
80          * Assigned branch office (if apply-able)
81          */
82         private BranchOffice departmentBranchOffice;
83
84         /**
85          * Assigned company for this department
86          */
87         private BasicData departmentCompany;
88
89         /**
90          * Assigned headquarter (if apply-able)
91          */
92         private Headquarter departmentHeadquarter;
93
94         /**
95          * Department name
96          */
97         private String departmentI18nKey;
98
99         /**
100          * Primary key of department
101          */
102         private Long departmentId;
103
104         /**
105          * Lead person of this department
106          */
107         private Contact departmentLead;
108
109         /**
110          * A general department controller (backing bean)
111          */
112         @Inject
113         private FinancialsDepartmentListWebViewController departmentListController;
114
115         /**
116          * Owning user instance (which this department is assigned to)
117          */
118         private User departmentUserOwner;
119
120         /**
121          * Event being fired when a department has been updated
122          */
123         @Inject
124         @Any
125         private Event<ObservableAdminDepartmentUpdatedEvent> updatedDepartmentEvent;
126
127         /**
128          * Default constructor
129          */
130         public FinancialsAdminDepartmentActionWebRequestBean () {
131                 // Call super constructor
132                 super();
133         }
134
135         /**
136          * Adds department with all data from this backing bean. First this action
137          * method will validate if the department's address is already registered
138          * and if found, it will output a proper faces message.
139          */
140         public void addDepartment () {
141                 // Get instance
142                 final Department department = this.createDepartment();
143
144                 // Is the department not created yet?
145                 if (this.departmentListController.isDepartmentAlreadyAdded(department)) {
146                         // Then show proper faces message
147                         this.showFacesMessage("form-admin-add-department:branchStreet", "ADMIN_DEPARTMENT_ALREADY_CREATED", FacesMessage.SEVERITY_WARN); //NOI18N
148                         return;
149                 }
150
151                 // Delcare updated instance
152                 final Department updatedDepartment;
153
154                 try {
155                         // Try to call EJB
156                         updatedDepartment = this.adminDepartmentBean.addDepartment(department);
157                 } catch (final DepartmentAlreadyAddedException ex) {
158                         // Output message
159                         this.showFacesMessage("form-admin-add-department:departmentI18nKey", "ADMIN_DEPARTMENT_ALREADY_CREATED", FacesMessage.SEVERITY_ERROR); //NOI18N
160                         return;
161                 }
162
163                 // Fire event
164                 this.departmentAddedEvent.fire(new AdminDepartmentAddedEvent(updatedDepartment));
165         }
166
167         /**
168          * Copies all properties from current department to this bean.
169          */
170         public void copyAllDepartmentProperties () {
171                 // Is current department set?
172                 if (this.getCurrentDepartment() == null) {
173                         // Throw NPE
174                         throw new NullPointerException("this.currentDepartment is null"); //NOI18N
175                 } else if (this.getCurrentDepartment().getDepartmentId() == null) {
176                         // Throw NPE
177                         throw new NullPointerException("this.currentDepartment.departmentId is null"); //NOI18N
178                 } else if (this.getCurrentDepartment().getDepartmentId() < 1) {
179                         // Throw IAE
180                         throw new IllegalArgumentException(MessageFormat.format("this.currentDepartment.departmentId={0} is not valid", this.getCurrentDepartment().getDepartmentId())); //NOI18N
181                 }
182
183                 // Copy all fields
184                 this.setDepartmentBranchOffice(this.getCurrentDepartment().getDepartmentBranchOffice());
185                 this.setDepartmentCompany(this.getCurrentDepartment().getDepartmentCompany());
186                 this.setDepartmentHeadquarter(this.getCurrentDepartment().getDepartmentHeadquarter());
187                 this.setDepartmentI18nKey(this.getCurrentDepartment().getDepartmentI18nKey());
188                 this.setDepartmentId(this.getCurrentDepartment().getDepartmentId());
189                 this.setDepartmentLead(this.getCurrentDepartment().getDepartmentLead());
190                 this.setDepartmentUserOwner(this.getCurrentDepartment().getDepartmentUserOwner());
191         }
192
193         /**
194          * Getter for current department
195          * <p>
196          * @return Current department
197          */
198         public Department getCurrentDepartment () {
199                 return this.currentDepartment;
200         }
201
202         /**
203          * Setter for current department
204          * <p>
205          * @param currentDepartment Current department
206          */
207         public void setCurrentDepartment (final Department currentDepartment) {
208                 this.currentDepartment = currentDepartment;
209         }
210
211         /**
212          * Getter for assigned branch office
213          * <p>
214          * @return Branch office
215          */
216         public BranchOffice getDepartmentBranchOffice () {
217                 return this.departmentBranchOffice;
218         }
219
220         /**
221          * Setter for assigned branch office
222          * <p>
223          * @param departmentDepartment Branch office
224          */
225         public void setDepartmentBranchOffice (final BranchOffice departmentDepartment) {
226                 this.departmentBranchOffice = departmentDepartment;
227         }
228
229         /**
230          * Getter for basic company data
231          * <p>
232          * @return Basic company data
233          */
234         public BasicData getDepartmentCompany () {
235                 return this.departmentCompany;
236         }
237
238         /**
239          * Setter for basic company data
240          * <p>
241          * @param departmentCompany Basic company data
242          */
243         public void setDepartmentCompany (final BasicData departmentCompany) {
244                 this.departmentCompany = departmentCompany;
245         }
246
247         /**
248          * Getter for assigned headquarter data
249          * <p>
250          * @return Headquarter data
251          */
252         public Headquarter getDepartmentHeadquarter () {
253                 return this.departmentHeadquarter;
254         }
255
256         /**
257          * Setter for assigned headquarter data
258          * <p>
259          * @param departmentHeadquarter Headquarter data
260          */
261         public void setDepartmentHeadquarter (final Headquarter departmentHeadquarter) {
262                 this.departmentHeadquarter = departmentHeadquarter;
263         }
264
265         /**
266          * Getter for department name
267          * <p>
268          * @return Department name
269          */
270         public String getDepartmentI18nKey () {
271                 return this.departmentI18nKey;
272         }
273
274         /**
275          * Setter for department name
276          * <p>
277          * @param departmentI18nKey Department name
278          */
279         public void setDepartmentI18nKey (final String departmentI18nKey) {
280                 this.departmentI18nKey = departmentI18nKey;
281         }
282
283         /**
284          * Getter for primary key
285          * <p>
286          * @return Primary key
287          */
288         public Long getDepartmentId () {
289                 return this.departmentId;
290         }
291
292         /**
293          * Setter for primary key
294          * <p>
295          * @param departmentId Primary key
296          */
297         public void setDepartmentId (final Long departmentId) {
298                 this.departmentId = departmentId;
299         }
300
301         /**
302          * Getter for department contact person
303          * <p>
304          * @return Department contact person
305          */
306         public Contact getDepartmentLead () {
307                 return this.departmentLead;
308         }
309
310         /**
311          * Setter for department contact person
312          * <p>
313          * @param departmentLead Department contact person
314          */
315         public void setDepartmentLead (final Contact departmentLead) {
316                 this.departmentLead = departmentLead;
317         }
318
319         /**
320          * Getter for owning user instance
321          * <p>
322          * @return Owning user instance
323          */
324         public User getDepartmentUserOwner () {
325                 return this.departmentUserOwner;
326         }
327
328         /**
329          * Setter for owning user instance
330          * <p>
331          * @param departmentUserOwner Owning user instance
332          */
333         public void setDepartmentUserOwner (final User departmentUserOwner) {
334                 this.departmentUserOwner = departmentUserOwner;
335         }
336
337         /**
338          * Updates department record with data from this bean.
339          * <p>
340          * @return Redirection outcome
341          */
342         public String updateDepartment () {
343                 // Is current department set?
344                 if (this.getCurrentDepartment() == null) {
345                         // Throw NPE
346                         throw new NullPointerException("this.currentDepartment is null"); //NOI18N
347                 } else if (this.getCurrentDepartment().getDepartmentId() == null) {
348                         // Throw NPE
349                         throw new NullPointerException("this.currentDepartment.departmentId is null"); //NOI18N
350                 } else if (this.getCurrentDepartment().getDepartmentId() < 1) {
351                         // Throw IAE
352                         throw new IllegalArgumentException(MessageFormat.format("this.currentDepartment.departmentId={0} is not valid", this.getCurrentDepartment().getDepartmentId())); //NOI18N
353                 }
354
355                 // Init instance with current data
356                 final Department department = this.createDepartment();
357
358                 // Does current (not updated) and just created (maybe updated) match?
359                 if (Objects.equals(this.getCurrentDepartment(), department)) {
360                         // Yes, then output message
361                         this.showFacesMessage("form-admin-edit-department:departmentI18nKey", "ADMIN_DEPARTMENT_NOT_UPDATED", FacesMessage.SEVERITY_WARN); //NOI18N
362
363                         // Skip below code
364                         return ""; //NOI18N
365                 }
366
367                 // Copy all fields
368                 System.out.println("departmentLead=" + this.getCurrentDepartment().getDepartmentLead() + " - BEFORE!");
369                 Departments.copyDepartmentData(department, this.getCurrentDepartment());
370                 System.out.println("departmentLead=" + this.getCurrentDepartment().getDepartmentLead() + " - AFTER!");
371
372                 // Initialize updated instance
373                 final Department updatedDepartment;
374
375                 // Try it
376                 try {
377                         // Invoke EJB
378                         updatedDepartment = this.adminDepartmentBean.updateDepartment(this.getCurrentDepartment());
379                 } catch (final DepartmentNotFoundException ex) {
380                         // Throw as a cause
381                         throw new FacesException(ex);
382                 }
383
384                 // Fire event
385                 this.updatedDepartmentEvent.fire(new AdminDepartmentUpdatedEvent(updatedDepartment));
386
387                 // Return to list view
388                 return "admin_list_departments"; //NOI18N
389         }
390
391         /**
392          * Prepares an instance of a Department object (entity) with all data from
393          * this bean. If a complete fax number or land-line number was provided, it
394          * will be set in the instance as well.
395          * <p>
396          * @return An instance of a Department class (entity)
397          */
398         private Department createDepartment () {
399                 // Create new department instance
400                 final Department department = new BusinessDepartment(this.getDepartmentCompany(), this.getDepartmentI18nKey());
401
402                 // Add all optional fields
403                 department.setDepartmentBranchOffice(this.getDepartmentBranchOffice());
404                 department.setDepartmentHeadquarter(this.getDepartmentHeadquarter());
405                 department.setDepartmentId(this.getDepartmentId());
406                 System.out.println("this.departmentLead=" + this.getDepartmentLead());
407                 department.setDepartmentLead(this.getDepartmentLead());
408                 department.setDepartmentUserOwner(this.getDepartmentUserOwner());
409
410                 // Return fully prepared instance
411                 return department;
412         }
413
414 }