001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.control.user.party.server.command;
018
019import com.echothree.control.user.party.common.form.CreateDepartmentForm;
020import com.echothree.control.user.party.common.result.CreateDepartmentResult;
021import com.echothree.control.user.party.common.result.PartyResultFactory;
022import com.echothree.model.control.accounting.server.control.AccountingControl;
023import com.echothree.model.control.party.common.PartyTypes;
024import com.echothree.model.control.party.server.control.PartyControl;
025import com.echothree.model.data.accounting.server.entity.Currency;
026import com.echothree.model.data.party.server.entity.DateTimeFormat;
027import com.echothree.model.data.party.server.entity.Language;
028import com.echothree.model.data.party.server.entity.Party;
029import com.echothree.model.data.party.server.entity.PartyCompany;
030import com.echothree.model.data.party.server.entity.PartyDepartment;
031import com.echothree.model.data.party.server.entity.PartyDivision;
032import com.echothree.model.data.party.server.entity.PartyType;
033import com.echothree.model.data.party.server.entity.TimeZone;
034import com.echothree.model.data.user.common.pk.UserVisitPK;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.common.validation.FieldDefinition;
037import com.echothree.util.common.validation.FieldType;
038import com.echothree.util.common.command.BaseResult;
039import com.echothree.util.common.persistence.BasePK;
040import com.echothree.util.server.control.BaseSimpleCommand;
041import com.echothree.util.server.persistence.Session;
042import java.util.Arrays;
043import java.util.Collections;
044import java.util.List;
045
046public class CreateDepartmentCommand
047        extends BaseSimpleCommand<CreateDepartmentForm> {
048    
049    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
050    
051    static {
052        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
053                new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, true, null, null),
054                new FieldDefinition("DivisionName", FieldType.ENTITY_NAME, true, null, null),
055                new FieldDefinition("DepartmentName", FieldType.ENTITY_NAME, true, null, null),
056                new FieldDefinition("Name", FieldType.STRING, true, 1L, 60L),
057                new FieldDefinition("PreferredLanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
058                new FieldDefinition("PreferredCurrencyIsoName", FieldType.ENTITY_NAME, false, null, null),
059                new FieldDefinition("PreferredJavaTimeZoneName", FieldType.TIME_ZONE_NAME, false, null, null),
060                new FieldDefinition("PreferredDateTimeFormatName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
062                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
063                ));
064    }
065    
066    /** Creates a new instance of CreateDepartmentCommand */
067    public CreateDepartmentCommand(UserVisitPK userVisitPK, CreateDepartmentForm form) {
068        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
069    }
070    
071    @Override
072    protected BaseResult execute() {
073        CreateDepartmentResult result = PartyResultFactory.getCreateDepartmentResult();
074        var partyControl = Session.getModelController(PartyControl.class);
075        String companyName = form.getCompanyName();
076        PartyCompany partyCompany = partyControl.getPartyCompanyByName(companyName);
077        
078        if(partyCompany != null) {
079            String divisionName = form.getDivisionName();
080            Party partyCompanyParty = partyCompany.getParty();
081            PartyDivision partyDivision = partyControl.getPartyDivisionByName(partyCompanyParty, divisionName);
082            
083            if(partyDivision != null) {
084                String departmentName = form.getDepartmentName();
085                Party partyDivisionParty = partyDivision.getParty();
086                PartyDepartment partyDepartment = partyControl.getPartyDepartmentByName(partyDivisionParty, departmentName);
087                
088                if(partyDepartment == null) {
089                    String preferredLanguageIsoName = form.getPreferredLanguageIsoName();
090                    Language preferredLanguage = preferredLanguageIsoName == null? null: partyControl.getLanguageByIsoName(preferredLanguageIsoName);
091                    
092                    if(preferredLanguageIsoName == null || (preferredLanguage != null)) {
093                        String preferredJavaTimeZoneName = form.getPreferredJavaTimeZoneName();
094                        TimeZone preferredTimeZone = preferredJavaTimeZoneName == null? null: partyControl.getTimeZoneByJavaName(preferredJavaTimeZoneName);
095                        
096                        if(preferredJavaTimeZoneName == null || (preferredTimeZone != null)) {
097                            String preferredDateTimeFormatName = form.getPreferredDateTimeFormatName();
098                            DateTimeFormat preferredDateTimeFormat = preferredDateTimeFormatName == null? null: partyControl.getDateTimeFormatByName(preferredDateTimeFormatName);
099                            
100                            if(preferredDateTimeFormatName == null || (preferredDateTimeFormat != null)) {
101                                String preferredCurrencyIsoName = form.getPreferredCurrencyIsoName();
102                                Currency preferredCurrency;
103                                
104                                if(preferredCurrencyIsoName == null)
105                                    preferredCurrency = null;
106                                else {
107                                    var accountingControl = Session.getModelController(AccountingControl.class);
108                                    preferredCurrency = accountingControl.getCurrencyByIsoName(preferredCurrencyIsoName);
109                                }
110                                
111                                if(preferredCurrencyIsoName == null || (preferredCurrency != null)) {
112                                    PartyType partyType = partyControl.getPartyTypeByName(PartyTypes.DEPARTMENT.name());
113                                    BasePK createdBy = getPartyPK();
114                                    String name = form.getName();
115                                    var isDefault = Boolean.valueOf(form.getIsDefault());
116                                    var sortOrder = Integer.valueOf(form.getSortOrder());
117                                    
118                                    Party party = partyControl.createParty(null, partyType, preferredLanguage, preferredCurrency, preferredTimeZone, preferredDateTimeFormat, createdBy);
119                                    partyControl.createPartyGroup(party, name, createdBy);
120                                    partyDepartment = partyControl.createPartyDepartment(party, partyDivisionParty, departmentName, isDefault, sortOrder, createdBy);
121                                } else {
122                                    addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), preferredCurrencyIsoName);
123                                }
124                            } else {
125                                addExecutionError(ExecutionErrors.UnknownDateTimeFormatName.name(), preferredDateTimeFormatName);
126                            }
127                        } else {
128                            addExecutionError(ExecutionErrors.UnknownJavaTimeZoneName.name(), preferredJavaTimeZoneName);
129                        }
130                    } else {
131                        addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), preferredLanguageIsoName);
132                    }
133                } else {
134                    addExecutionError(ExecutionErrors.DuplicateDepartmentName.name(), departmentName);
135                }
136                
137                if(partyDepartment != null) {
138                    Party party = partyDepartment.getParty();
139                    
140                    result.setEntityRef(party.getPrimaryKey().getEntityRef());
141                    result.setDepartmentName(partyDepartment.getPartyDepartmentName());
142                    result.setPartyName(party.getLastDetail().getPartyName());
143                }
144            } else {
145                addExecutionError(ExecutionErrors.UnknownDivisionName.name(), divisionName);
146            }
147        } else {
148            addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName);
149        }
150        
151        return result;
152    }
153    
154}