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.edit.DepartmentEdit;
020import com.echothree.control.user.party.common.edit.PartyEditFactory;
021import com.echothree.control.user.party.common.form.EditDepartmentForm;
022import com.echothree.control.user.party.common.result.EditDepartmentResult;
023import com.echothree.control.user.party.common.result.PartyResultFactory;
024import com.echothree.control.user.party.common.spec.DepartmentSpec;
025import com.echothree.model.control.accounting.server.control.AccountingControl;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.data.accounting.server.entity.Currency;
028import com.echothree.model.data.party.common.pk.PartyPK;
029import com.echothree.model.data.party.server.entity.DateTimeFormat;
030import com.echothree.model.data.party.server.entity.Language;
031import com.echothree.model.data.party.server.entity.Party;
032import com.echothree.model.data.party.server.entity.PartyCompany;
033import com.echothree.model.data.party.server.entity.PartyDepartment;
034import com.echothree.model.data.party.server.entity.PartyDetail;
035import com.echothree.model.data.party.server.entity.PartyDivision;
036import com.echothree.model.data.party.server.entity.PartyGroup;
037import com.echothree.model.data.party.server.entity.TimeZone;
038import com.echothree.model.data.party.server.value.PartyDepartmentValue;
039import com.echothree.model.data.party.server.value.PartyDetailValue;
040import com.echothree.model.data.party.server.value.PartyGroupValue;
041import com.echothree.model.data.user.common.pk.UserVisitPK;
042import com.echothree.util.common.message.ExecutionErrors;
043import com.echothree.util.common.validation.FieldDefinition;
044import com.echothree.util.common.validation.FieldType;
045import com.echothree.util.common.command.BaseResult;
046import com.echothree.util.common.command.EditMode;
047import com.echothree.util.server.control.BaseEditCommand;
048import com.echothree.util.server.persistence.Session;
049import java.util.Arrays;
050import java.util.Collections;
051import java.util.List;
052
053public class EditDepartmentCommand
054        extends BaseEditCommand<DepartmentSpec, DepartmentEdit> {
055    
056    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
057    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
058    
059    static {
060        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
061                new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("DivisionName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("DepartmentName", FieldType.ENTITY_NAME, true, null, null)
064                ));
065        
066        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
067                new FieldDefinition("DepartmentName", FieldType.ENTITY_NAME, true, null, null),
068                new FieldDefinition("Name", FieldType.STRING, true, 1L, 60L),
069                new FieldDefinition("PreferredLanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
070                new FieldDefinition("PreferredCurrencyIsoName", FieldType.ENTITY_NAME, false, null, null),
071                new FieldDefinition("PreferredJavaTimeZoneName", FieldType.TIME_ZONE_NAME, false, null, null),
072                new FieldDefinition("PreferredDateTimeFormatName", FieldType.ENTITY_NAME, false, null, null),
073                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
074                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
075                ));
076    }
077    
078    /** Creates a new instance of EditDepartmentCommand */
079    public EditDepartmentCommand(UserVisitPK userVisitPK, EditDepartmentForm form) {
080        super(userVisitPK, form, null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
081    }
082    
083    @Override
084    protected BaseResult execute() {
085        var partyControl = Session.getModelController(PartyControl.class);
086        EditDepartmentResult result = PartyResultFactory.getEditDepartmentResult();
087        String companyName = spec.getCompanyName();
088        PartyCompany partyCompany = partyControl.getPartyCompanyByName(companyName);
089        
090        if(partyCompany != null) {
091            String divisionName = spec.getDivisionName();
092            Party partyCompanyParty = partyCompany.getParty();
093            PartyDivision partyDivision = partyControl.getPartyDivisionByName(partyCompanyParty, divisionName);
094            
095            if(partyDivision != null) {
096                String originalDepartmentName = spec.getDepartmentName();
097                Party partyDivisionParty = partyDivision.getParty();
098                PartyDepartment partyDepartment = partyControl.getPartyDepartmentByNameForUpdate(partyDivisionParty, originalDepartmentName);
099                
100                if(partyDepartment != null) {
101                    Party party = partyDepartment.getParty();
102                    
103                    if(editMode.equals(EditMode.LOCK)) {
104                        result.setDepartment(partyControl.getDepartmentTransfer(getUserVisit(), partyDepartment));
105                        
106                        if(lockEntity(party)) {
107                            DepartmentEdit edit = PartyEditFactory.getDepartmentEdit();
108                            PartyDetail partyDetail = party.getLastDetail();
109                            PartyGroup partyGroup = partyControl.getPartyGroup(party);
110                            Language preferredLanguage = partyDetail.getPreferredLanguage();
111                            Currency preferredCurrency = partyDetail.getPreferredCurrency();
112                            TimeZone preferredTimeZone = partyDetail.getPreferredTimeZone();
113                            DateTimeFormat preferredDateTimeFormat = partyDetail.getPreferredDateTimeFormat();
114                            
115                            result.setEdit(edit);
116                            edit.setDepartmentName(partyDepartment.getPartyDepartmentName());
117                            edit.setName(partyGroup.getName());
118                            edit.setPreferredLanguageIsoName(preferredLanguage == null? null: preferredLanguage.getLanguageIsoName());
119                            edit.setPreferredCurrencyIsoName(preferredCurrency == null? null: preferredCurrency.getCurrencyIsoName());
120                            edit.setPreferredJavaTimeZoneName(preferredTimeZone == null? null: preferredTimeZone.getLastDetail().getJavaTimeZoneName());
121                            edit.setPreferredDateTimeFormatName(preferredDateTimeFormat == null? null: preferredDateTimeFormat.getLastDetail().getDateTimeFormatName());
122                            edit.setIsDefault(partyDepartment.getIsDefault().toString());
123                            edit.setSortOrder(partyDepartment.getSortOrder().toString());
124                        } else {
125                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
126                        }
127                        
128                        result.setEntityLock(getEntityLockTransfer(party));
129                    } else if(editMode.equals(EditMode.ABANDON)) {
130                        unlockEntity(party);
131                    } else if(editMode.equals(EditMode.UPDATE)) {
132                        PartyDepartmentValue partyDepartmentValue = partyControl.getPartyDepartmentValueForUpdate(partyDepartment);
133                        String departmentName = edit.getDepartmentName();
134                        PartyDepartment duplicatePartyDepartment = partyControl.getPartyDepartmentByName(partyDivisionParty, departmentName);
135                        
136                        if(duplicatePartyDepartment == null || duplicatePartyDepartment.getPrimaryKey().equals(partyDepartmentValue.getPrimaryKey())) {
137                            String preferredLanguageIsoName = edit.getPreferredLanguageIsoName();
138                            Language preferredLanguage = preferredLanguageIsoName == null? null: partyControl.getLanguageByIsoName(preferredLanguageIsoName);
139                            
140                            if(preferredLanguageIsoName == null || (preferredLanguage != null)) {
141                                String preferredJavaTimeZoneName = edit.getPreferredJavaTimeZoneName();
142                                TimeZone preferredTimeZone = preferredJavaTimeZoneName == null? null: partyControl.getTimeZoneByJavaName(preferredJavaTimeZoneName);
143                                
144                                if(preferredJavaTimeZoneName == null || (preferredTimeZone != null)) {
145                                    String preferredDateTimeFormatName = edit.getPreferredDateTimeFormatName();
146                                    DateTimeFormat preferredDateTimeFormat = preferredDateTimeFormatName == null? null: partyControl.getDateTimeFormatByName(preferredDateTimeFormatName);
147                                    
148                                    if(preferredDateTimeFormatName == null || (preferredDateTimeFormat != null)) {
149                                        String preferredCurrencyIsoName = edit.getPreferredCurrencyIsoName();
150                                        Currency preferredCurrency;
151                                        
152                                        if(preferredCurrencyIsoName == null)
153                                            preferredCurrency = null;
154                                        else {
155                                            var accountingControl = Session.getModelController(AccountingControl.class);
156                                            preferredCurrency = accountingControl.getCurrencyByIsoName(preferredCurrencyIsoName);
157                                        }
158                                        
159                                        if(preferredCurrencyIsoName == null || (preferredCurrency != null)) {
160                                            if(lockEntityForUpdate(party)) {
161                                                try {
162                                                    PartyPK updatedBy = getPartyPK();
163                                                    PartyDetailValue partyDetailValue = partyControl.getPartyDetailValueForUpdate(party);
164                                                    PartyGroupValue partyGroupValue = partyControl.getPartyGroupValueForUpdate(party);
165                                                    
166                                                    partyDepartmentValue.setPartyDepartmentName(departmentName);
167                                                    partyGroupValue.setName(edit.getName());
168                                                    partyDepartmentValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
169                                                    partyDepartmentValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
170                                                    
171                                                    partyControl.updatePartyDepartmentFromValue(partyDepartmentValue, updatedBy);
172                                                    partyControl.updatePartyFromValue(partyDetailValue, updatedBy);
173                                                    partyControl.updatePartyGroupFromValue(partyGroupValue, updatedBy);
174                                                } finally {
175                                                    unlockEntity(party);
176                                                }
177                                            } else {
178                                                addExecutionError(ExecutionErrors.EntityLockStale.name());
179                                            }
180                                        } else {
181                                            addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), preferredCurrencyIsoName);
182                                        }
183                                    } else {
184                                        addExecutionError(ExecutionErrors.UnknownDateTimeFormatName.name(), preferredDateTimeFormatName);
185                                    }
186                                } else {
187                                    addExecutionError(ExecutionErrors.UnknownJavaTimeZoneName.name(), preferredJavaTimeZoneName);
188                                }
189                            } else {
190                                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), preferredLanguageIsoName);
191                            }
192                        } else {
193                            addExecutionError(ExecutionErrors.DuplicateDepartmentName.name(), departmentName);
194                        }
195                        
196                        if(hasExecutionErrors()) {
197                            result.setDepartment(partyControl.getDepartmentTransfer(getUserVisit(), partyDepartment));
198                            result.setEntityLock(getEntityLockTransfer(party));
199                        }
200                    }
201                } else {
202                    addExecutionError(ExecutionErrors.UnknownDepartmentName.name(), originalDepartmentName);
203                }
204            } else {
205                addExecutionError(ExecutionErrors.UnknownDivisionName.name(), divisionName);
206            }
207        } else {
208            addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName);
209        }
210        
211        return result;
212    }
213    
214}