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.CreateCompanyForm;
020import com.echothree.control.user.party.common.result.CreateCompanyResult;
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.PartyType;
031import com.echothree.model.data.party.server.entity.TimeZone;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.common.command.BaseResult;
037import com.echothree.util.common.persistence.BasePK;
038import com.echothree.util.server.control.BaseSimpleCommand;
039import com.echothree.util.server.persistence.Session;
040import java.util.Arrays;
041import java.util.Collections;
042import java.util.List;
043
044public class CreateCompanyCommand
045        extends BaseSimpleCommand<CreateCompanyForm> {
046    
047    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
048    
049    static {
050        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
051                new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, true, null, null),
052                new FieldDefinition("Name", FieldType.STRING, true, 1L, 60L),
053                new FieldDefinition("PreferredLanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
054                new FieldDefinition("PreferredCurrencyIsoName", FieldType.ENTITY_NAME, false, null, null),
055                new FieldDefinition("PreferredJavaTimeZoneName", FieldType.TIME_ZONE_NAME, false, null, null),
056                new FieldDefinition("PreferredDateTimeFormatName", FieldType.ENTITY_NAME, false, null, null),
057                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
058                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
059                ));
060    }
061    
062    /** Creates a new instance of CreateCompanyCommand */
063    public CreateCompanyCommand(UserVisitPK userVisitPK, CreateCompanyForm form) {
064        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
065    }
066    
067    @Override
068    protected BaseResult execute() {
069        CreateCompanyResult result = PartyResultFactory.getCreateCompanyResult();
070        var partyControl = Session.getModelController(PartyControl.class);
071        String companyName = form.getCompanyName();
072        PartyCompany partyCompany = partyControl.getPartyCompanyByName(companyName);
073        
074        if(partyCompany == null) {
075            String preferredLanguageIsoName = form.getPreferredLanguageIsoName();
076            Language preferredLanguage = preferredLanguageIsoName == null? null: partyControl.getLanguageByIsoName(preferredLanguageIsoName);
077            
078            if(preferredLanguageIsoName == null || (preferredLanguage != null)) {
079                String preferredJavaTimeZoneName = form.getPreferredJavaTimeZoneName();
080                TimeZone preferredTimeZone = preferredJavaTimeZoneName == null? null: partyControl.getTimeZoneByJavaName(preferredJavaTimeZoneName);
081                
082                if(preferredJavaTimeZoneName == null || (preferredTimeZone != null)) {
083                    String preferredDateTimeFormatName = form.getPreferredDateTimeFormatName();
084                    DateTimeFormat preferredDateTimeFormat = preferredDateTimeFormatName == null? null: partyControl.getDateTimeFormatByName(preferredDateTimeFormatName);
085                    
086                    if(preferredDateTimeFormatName == null || (preferredDateTimeFormat != null)) {
087                        String preferredCurrencyIsoName = form.getPreferredCurrencyIsoName();
088                        Currency preferredCurrency;
089                        
090                        if(preferredCurrencyIsoName == null)
091                            preferredCurrency = null;
092                        else {
093                            var accountingControl = Session.getModelController(AccountingControl.class);
094                            preferredCurrency = accountingControl.getCurrencyByIsoName(preferredCurrencyIsoName);
095                        }
096                        
097                        if(preferredCurrencyIsoName == null || (preferredCurrency != null)) {
098                            PartyType partyType = partyControl.getPartyTypeByName(PartyTypes.COMPANY.name());
099                            BasePK createdBy = getPartyPK();
100                            String name = form.getName();
101                            var isDefault = Boolean.valueOf(form.getIsDefault());
102                            var sortOrder = Integer.valueOf(form.getSortOrder());
103                            
104                            Party party = partyControl.createParty(null, partyType, preferredLanguage, preferredCurrency, preferredTimeZone, preferredDateTimeFormat, createdBy);
105                            partyControl.createPartyGroup(party, name, createdBy);
106                            partyCompany = partyControl.createPartyCompany(party, companyName, isDefault, sortOrder, createdBy);
107                        } else {
108                            addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), preferredCurrencyIsoName);
109                        }
110                    } else {
111                        addExecutionError(ExecutionErrors.UnknownDateTimeFormatName.name(), preferredDateTimeFormatName);
112                    }
113                } else {
114                    addExecutionError(ExecutionErrors.UnknownJavaTimeZoneName.name(), preferredJavaTimeZoneName);
115                }
116            } else {
117                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), preferredLanguageIsoName);
118            }
119        } else {
120            addExecutionError(ExecutionErrors.DuplicateCompanyName.name(), companyName);
121        }
122        
123        if(partyCompany != null) {
124            Party party = partyCompany.getParty();
125            
126            result.setEntityRef(party.getPrimaryKey().getEntityRef());
127            result.setCompanyName(partyCompany.getPartyCompanyName());
128            result.setPartyName(party.getLastDetail().getPartyName());
129        }
130        
131        return result;
132    }
133    
134}