+/*
+ * Copyright (C) 2017 Roland Häder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcontactsbusiness.branchoffice;
+
+import java.text.MessageFormat;
+import java.util.GregorianCalendar;
+import java.util.List;
+import javax.ejb.EJB;
+import javax.ejb.Stateless;
+import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
+import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeAlreadyAddedException;
+import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
+import org.mxchange.jcontactsbusiness.model.branchoffice.AdminBranchOfficeSessionBeanRemote;
+import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
+import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote;
+import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffices;
+import org.mxchange.jcountry.model.data.Country;
+import org.mxchange.jusercore.model.user.User;
+
+/**
+ * A stateless session bean for administrative branch office purposes
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@Stateless (name = "adminBranchOffice", description = "An administrative statless bean for handling branch office data (all)")
+public class AddressbookAdminBranchOfficeSessionBean extends BaseAddressbookDatabaseBean implements AdminBranchOfficeSessionBeanRemote {
+
+ /**
+ * Serial number
+ */
+ private static final long serialVersionUID = 58_467_386_571_701L;
+
+ /**
+ * General branch office bean
+ */
+ @EJB
+ private BranchOfficeSessionBeanRemote branchOfficeBean;
+
+ /**
+ * Default constructor
+ */
+ public AddressbookAdminBranchOfficeSessionBean () {
+ // Call super constructor
+ super();
+ }
+
+ @Override
+ public BranchOffice addBranchOffice (final BranchOffice branchOffice) throws BranchOfficeAlreadyAddedException {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
+
+ // Validate parameter
+ if (null == branchOffice) {
+ // Throw NPE
+ throw new NullPointerException("branchOffice is null"); //NOI18N
+ } else if (branchOffice.getBranchId() instanceof Long) {
+ // Should not happen
+ throw new IllegalArgumentException("branchOffice.branchId should not be set."); //NOI18N
+ } else if (this.isBranchOfficeFound(branchOffice)) {
+ // Already added, abort here
+ throw new BranchOfficeAlreadyAddedException(branchOffice);
+ }
+
+ // Add created timestamp
+ branchOffice.setBranchCreated(new GregorianCalendar());
+
+ // Is user instance set?
+ if (branchOffice.getBranchCompany() instanceof BusinessBasicData) {
+ // Get managed instance back
+ final BusinessBasicData managedBasicData = this.createManaged(branchOffice.getBranchCompany());
+
+ // Set it back in branch office
+ branchOffice.setBranchCompany(managedBasicData);
+ }
+
+ // Is user instance set?
+ if (branchOffice.getBranchUserOwner() instanceof User) {
+ // Get managed instance back
+ final User managedUser = this.createManaged(branchOffice.getBranchUserOwner());
+
+ // Set it back in branch office
+ branchOffice.setBranchUserOwner(managedUser);
+ }
+
+ // Is user instance set?
+ if (branchOffice.getBranchCountry() instanceof Country) {
+ // Get managed instance back
+ final Country managedCountry = this.createManaged(branchOffice.getBranchCountry());
+
+ // Set it back in branch office
+ branchOffice.setBranchCountry(managedCountry);
+ }
+
+ // Persist it
+ this.getEntityManager().persist(branchOffice);
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchId={1} - EXIT!", this.getClass().getSimpleName(), branchOffice.getBranchId())); //NOI18N
+
+ // Return updated instance
+ return branchOffice;
+ }
+
+ /**
+ * Checks if given branch office's address is already persisted. The whole
+ * (persisted) list is being loaded and each address is being matched
+ * against the given branch office's address.
+ * <p>
+ * @param branchOffice Branch office being checked
+ * <p>
+ * @return Whether it has been found
+ */
+ private boolean isBranchOfficeFound (final BranchOffice branchOffice) {
+ // Get whole list
+ final List<BranchOffice> branchOffices = this.branchOfficeBean.allBranchOffices();
+
+ // Default is not found
+ boolean isFound = false;
+
+ // Check all single addresses
+ for (final BranchOffice bo : branchOffices) {
+ // Is the same address found?
+ if (BranchOffices.isSameAddress(bo, branchOffice)) {
+ // Found one
+ isFound = true;
+ break;
+ }
+ }
+
+ // Return flag
+ return isFound;
+ }
+
+}