2 * Copyright (C) 2017 - 2022 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.addressbook.beans.business.department.action;
19 import java.text.MessageFormat;
20 import java.util.Objects;
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.addressbook.beans.BaseAddressbookBean;
30 import org.mxchange.addressbook.beans.business.department.list.AddressbookDepartmentListWebViewController;
31 import org.mxchange.jcontacts.model.contact.Contact;
32 import org.mxchange.jcontactsbusiness.events.department.added.AdminDepartmentAddedEvent;
33 import org.mxchange.jcontactsbusiness.events.department.added.ObservableAdminDepartmentAddedEvent;
34 import org.mxchange.jcontactsbusiness.events.department.updated.AdminDepartmentUpdatedEvent;
35 import org.mxchange.jcontactsbusiness.events.department.updated.ObservableAdminDepartmentUpdatedEvent;
36 import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentAlreadyAddedException;
37 import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentNotFoundException;
38 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
39 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
40 import org.mxchange.jcontactsbusiness.model.department.AdminDepartmentSessionBeanRemote;
41 import org.mxchange.jcontactsbusiness.model.department.BusinessDepartment;
42 import org.mxchange.jcontactsbusiness.model.department.Department;
43 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
44 import org.mxchange.jcontactsbusiness.model.utils.DepartmentUtils;
45 import org.mxchange.jusercore.model.user.User;
48 * An administrative action bean for departments
50 * @author Roland Häder<roland@mxchange.org>
52 @Named ("adminDepartmentActionController")
54 public class AddressbookAdminDepartmentActionWebRequestBean extends BaseAddressbookBean implements AddressbookAdminDepartmentActionWebRequestController {
59 private static final long serialVersionUID = 5_028_697_360_473L;
62 * EJB for administrative purposes
64 @EJB (lookup = "java:global/addressbook-ejb/adminDepartment!org.mxchange.jcontactsbusiness.model.department.AdminDepartmentSessionBeanRemote")
65 private AdminDepartmentSessionBeanRemote adminDepartmentBean;
68 * Currently worked on department
70 private Department currentDepartment;
73 * An event being fired when a department has been successfully added
77 private Event<ObservableAdminDepartmentAddedEvent> departmentAddedEvent;
80 * Assigned branch office (if apply-able)
82 private BranchOffice departmentBranchOffice;
85 * Assigned company for this department
87 private BasicData departmentCompany;
90 * Assigned headquarter (if apply-able)
92 private Headquarter departmentHeadquarter;
97 private String departmentI18nKey;
100 * Primary key of department
102 private Long departmentId;
105 * Lead person of this department
107 private Contact departmentLead;
110 * A general department controller (backing bean)
113 private AddressbookDepartmentListWebViewController departmentListController;
116 * Owning user instance (which this department is assigned to)
118 private User departmentUserOwner;
121 * Event being fired when a department has been updated
125 private Event<ObservableAdminDepartmentUpdatedEvent> updatedDepartmentEvent;
128 * Default constructor
130 public AddressbookAdminDepartmentActionWebRequestBean () {
131 // Call super constructor
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.
140 public void addDepartment () {
142 final Department department = this.createDepartment();
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
151 // Delcare updated instance
152 final Department updatedDepartment;
156 updatedDepartment = this.adminDepartmentBean.addDepartment(department);
157 } catch (final DepartmentAlreadyAddedException ex) {
159 this.showFacesMessage("form-admin-add-department:departmentI18nKey", "ADMIN_DEPARTMENT_ALREADY_CREATED", FacesMessage.SEVERITY_ERROR); //NOI18N
164 this.departmentAddedEvent.fire(new AdminDepartmentAddedEvent(updatedDepartment));
168 * Copies all properties from current department to this bean.
170 public void copyAllDepartmentProperties () {
171 // Is current department set?
172 if (this.getCurrentDepartment() == null) {
174 throw new NullPointerException("this.currentDepartment is null"); //NOI18N
175 } else if (this.getCurrentDepartment().getDepartmentId() == null) {
177 throw new NullPointerException("this.currentDepartment.departmentId is null"); //NOI18N
178 } else if (this.getCurrentDepartment().getDepartmentId() < 1) {
180 throw new IllegalArgumentException(MessageFormat.format("this.currentDepartment.departmentId={0} is not valid", this.getCurrentDepartment().getDepartmentId())); //NOI18N
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());
194 * Getter for current department
196 * @return Current department
198 public Department getCurrentDepartment () {
199 return this.currentDepartment;
203 * Setter for current department
205 * @param currentDepartment Current department
207 public void setCurrentDepartment (final Department currentDepartment) {
208 this.currentDepartment = currentDepartment;
212 * Getter for assigned branch office
214 * @return Branch office
216 public BranchOffice getDepartmentBranchOffice () {
217 return this.departmentBranchOffice;
221 * Setter for assigned branch office
223 * @param departmentDepartment Branch office
225 public void setDepartmentBranchOffice (final BranchOffice departmentDepartment) {
226 this.departmentBranchOffice = departmentDepartment;
230 * Getter for basic company data
232 * @return Basic company data
234 public BasicData getDepartmentCompany () {
235 return this.departmentCompany;
239 * Setter for basic company data
241 * @param departmentCompany Basic company data
243 public void setDepartmentCompany (final BasicData departmentCompany) {
244 this.departmentCompany = departmentCompany;
248 * Getter for assigned headquarter data
250 * @return Headquarter data
252 public Headquarter getDepartmentHeadquarter () {
253 return this.departmentHeadquarter;
257 * Setter for assigned headquarter data
259 * @param departmentHeadquarter Headquarter data
261 public void setDepartmentHeadquarter (final Headquarter departmentHeadquarter) {
262 this.departmentHeadquarter = departmentHeadquarter;
266 * Getter for department name
268 * @return Department name
270 public String getDepartmentI18nKey () {
271 return this.departmentI18nKey;
275 * Setter for department name
277 * @param departmentI18nKey Department name
279 public void setDepartmentI18nKey (final String departmentI18nKey) {
280 this.departmentI18nKey = departmentI18nKey;
284 * Getter for primary key
286 * @return Primary key
288 public Long getDepartmentId () {
289 return this.departmentId;
293 * Setter for primary key
295 * @param departmentId Primary key
297 public void setDepartmentId (final Long departmentId) {
298 this.departmentId = departmentId;
302 * Getter for department contact person
304 * @return Department contact person
306 public Contact getDepartmentLead () {
307 return this.departmentLead;
311 * Setter for department contact person
313 * @param departmentLead Department contact person
315 public void setDepartmentLead (final Contact departmentLead) {
316 this.departmentLead = departmentLead;
320 * Getter for owning user instance
322 * @return Owning user instance
324 public User getDepartmentUserOwner () {
325 return this.departmentUserOwner;
329 * Setter for owning user instance
331 * @param departmentUserOwner Owning user instance
333 public void setDepartmentUserOwner (final User departmentUserOwner) {
334 this.departmentUserOwner = departmentUserOwner;
338 * Updates department record with data from this bean.
340 * @return Redirection outcome
342 public String updateDepartment () {
343 // Is current department set?
344 if (this.getCurrentDepartment() == null) {
346 throw new NullPointerException("this.currentDepartment is null"); //NOI18N
347 } else if (this.getCurrentDepartment().getDepartmentId() == null) {
349 throw new NullPointerException("this.currentDepartment.departmentId is null"); //NOI18N
350 } else if (this.getCurrentDepartment().getDepartmentId() < 1) {
352 throw new IllegalArgumentException(MessageFormat.format("this.currentDepartment.departmentId={0} is not valid", this.getCurrentDepartment().getDepartmentId())); //NOI18N
355 // Init instance with current data
356 final Department department = this.createDepartment();
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
368 DepartmentUtils.copyDepartmentData(department, this.getCurrentDepartment());
370 // Initialize updated instance
371 final Department updatedDepartment;
376 updatedDepartment = this.adminDepartmentBean.updateDepartment(this.getCurrentDepartment());
377 } catch (final DepartmentNotFoundException ex) {
379 throw new FacesException(ex);
383 this.updatedDepartmentEvent.fire(new AdminDepartmentUpdatedEvent(updatedDepartment));
385 // Return to list view
386 return "admin_list_departments"; //NOI18N
390 * Prepares an instance of a Department object (entity) with all data from
391 * this bean. If a complete fax number or land-line number was provided, it
392 * will be set in the instance as well.
394 * @return An instance of a Department class (entity)
396 private Department createDepartment () {
397 // Create new department instance
398 final Department department = new BusinessDepartment(this.getDepartmentCompany(), this.getDepartmentI18nKey());
400 // Add all optional fields
401 department.setDepartmentBranchOffice(this.getDepartmentBranchOffice());
402 department.setDepartmentHeadquarter(this.getDepartmentHeadquarter());
403 department.setDepartmentId(this.getDepartmentId());
404 department.setDepartmentLead(this.getDepartmentLead());
405 department.setDepartmentUserOwner(this.getDepartmentUserOwner());
407 // Return fully prepared instance