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.accounting.server.command;
018
019import com.echothree.control.user.accounting.common.edit.AccountingEditFactory;
020import com.echothree.control.user.accounting.common.edit.GlAccountClassEdit;
021import com.echothree.control.user.accounting.common.form.EditGlAccountClassForm;
022import com.echothree.control.user.accounting.common.result.AccountingResultFactory;
023import com.echothree.control.user.accounting.common.result.EditGlAccountClassResult;
024import com.echothree.control.user.accounting.common.spec.GlAccountClassSpec;
025import com.echothree.model.control.accounting.server.control.AccountingControl;
026import com.echothree.model.control.core.common.EventTypes;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.accounting.server.entity.GlAccountClass;
031import com.echothree.model.data.accounting.server.entity.GlAccountClassDescription;
032import com.echothree.model.data.accounting.server.entity.GlAccountClassDetail;
033import com.echothree.model.data.accounting.server.value.GlAccountClassDescriptionValue;
034import com.echothree.model.data.accounting.server.value.GlAccountClassDetailValue;
035import com.echothree.model.data.user.common.pk.UserVisitPK;
036import com.echothree.util.common.message.ExecutionErrors;
037import com.echothree.util.common.validation.FieldDefinition;
038import com.echothree.util.common.validation.FieldType;
039import com.echothree.util.common.command.BaseResult;
040import com.echothree.util.common.command.EditMode;
041import com.echothree.util.server.control.BaseEditCommand;
042import com.echothree.util.server.control.CommandSecurityDefinition;
043import com.echothree.util.server.control.PartyTypeDefinition;
044import com.echothree.util.server.control.SecurityRoleDefinition;
045import com.echothree.util.server.persistence.Session;
046import java.util.Arrays;
047import java.util.Collections;
048import java.util.List;
049
050public class EditGlAccountClassCommand
051        extends BaseEditCommand<GlAccountClassSpec, GlAccountClassEdit> {
052    
053    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
054    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
055    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
056    
057    static {
058        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
059                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
060                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
061                    new SecurityRoleDefinition(SecurityRoleGroups.GlAccountClass.name(), SecurityRoles.Edit.name())
062                    )))
063                )));
064        
065        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
066                new FieldDefinition("GlAccountClassName", FieldType.ENTITY_NAME, true, null, null)
067                ));
068        
069        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
070                new FieldDefinition("GlAccountClassName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("ParentGlAccountClassName", FieldType.ENTITY_NAME, false, null, null),
072                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
073                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
074                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
075                ));
076    }
077    
078    /** Creates a new instance of EditGlAccountClassCommand */
079    public EditGlAccountClassCommand(UserVisitPK userVisitPK, EditGlAccountClassForm form) {
080        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
081    }
082    
083    @Override
084    protected BaseResult execute() {
085        var accountingControl = Session.getModelController(AccountingControl.class);
086        EditGlAccountClassResult result = AccountingResultFactory.getEditGlAccountClassResult();
087        
088        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
089            String glAccountClassName = spec.getGlAccountClassName();
090            GlAccountClass glAccountClass = accountingControl.getGlAccountClassByName(glAccountClassName);
091            
092            if(glAccountClass != null) {
093                if(editMode.equals(EditMode.LOCK)) {
094                    if(lockEntity(glAccountClass)) {
095                        GlAccountClassDescription glAccountClassDescription = accountingControl.getGlAccountClassDescription(glAccountClass, getPreferredLanguage());
096                        GlAccountClassEdit edit = AccountingEditFactory.getGlAccountClassEdit();
097                        GlAccountClassDetail glAccountClassDetail = glAccountClass.getLastDetail();
098                        GlAccountClass parentGlAccountClass = glAccountClassDetail.getParentGlAccountClass();
099
100                        result.setGlAccountClass(accountingControl.getGlAccountClassTransfer(getUserVisit(), glAccountClass));
101
102                        result.setEdit(edit);
103                        edit.setGlAccountClassName(glAccountClassDetail.getGlAccountClassName());
104                        edit.setParentGlAccountClassName(parentGlAccountClass == null? null: parentGlAccountClass.getLastDetail().getGlAccountClassName());
105                        edit.setIsDefault(glAccountClassDetail.getIsDefault().toString());
106                        edit.setSortOrder(glAccountClassDetail.getSortOrder().toString());
107
108                        if(glAccountClassDescription != null) {
109                            edit.setDescription(glAccountClassDescription.getDescription());
110                        }
111                    } else {
112                        addExecutionError(ExecutionErrors.EntityLockFailed.name());
113                    }
114
115                    result.setEntityLock(getEntityLockTransfer(glAccountClass));
116                } else { // EditMode.ABANDON
117                    unlockEntity(glAccountClass);
118                }
119            } else {
120                addExecutionError(ExecutionErrors.UnknownGlAccountClassName.name(), glAccountClassName);
121            }
122        } else if(editMode.equals(EditMode.UPDATE)) {
123            String glAccountClassName = spec.getGlAccountClassName();
124            GlAccountClass glAccountClass = accountingControl.getGlAccountClassByNameForUpdate(glAccountClassName);
125            
126            if(glAccountClass != null) {
127                glAccountClassName = edit.getGlAccountClassName();
128                GlAccountClass duplicateGlAccountClass = accountingControl.getGlAccountClassByName(glAccountClassName);
129                
130                if(duplicateGlAccountClass == null || glAccountClass.equals(duplicateGlAccountClass)) {
131                    String parentGlAccountClassName = edit.getParentGlAccountClassName();
132                    GlAccountClass parentGlAccountClass = null;
133                    
134                    if(parentGlAccountClassName != null) {
135                        parentGlAccountClass = accountingControl.getGlAccountClassByName(parentGlAccountClassName);
136                    }
137                    
138                    if(parentGlAccountClassName == null || parentGlAccountClass != null) {
139                        if(accountingControl.isParentGlAccountClassSafe(glAccountClass, parentGlAccountClass)) {
140                            if(lockEntityForUpdate(glAccountClass)) {
141                                try {
142                                    var partyPK = getPartyPK();
143                                    GlAccountClassDetailValue glAccountClassDetailValue = accountingControl.getGlAccountClassDetailValueForUpdate(glAccountClass);
144                                    GlAccountClassDescription glAccountClassDescription = accountingControl.getGlAccountClassDescriptionForUpdate(glAccountClass, getPreferredLanguage());
145                                    String description = edit.getDescription();
146                                    
147                                    glAccountClassDetailValue.setGlAccountClassName(edit.getGlAccountClassName());
148                                    glAccountClassDetailValue.setParentGlAccountClassPK(parentGlAccountClass == null? null: parentGlAccountClass.getPrimaryKey());
149                                    glAccountClassDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
150                                    glAccountClassDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
151                                    
152                                    accountingControl.updateGlAccountClassFromValue(glAccountClassDetailValue, partyPK);
153                                    
154                                    if(glAccountClassDescription == null && description != null) {
155                                        accountingControl.createGlAccountClassDescription(glAccountClass, getPreferredLanguage(), description, partyPK);
156                                    } else if(glAccountClassDescription != null && description == null) {
157                                        accountingControl.deleteGlAccountClassDescription(glAccountClassDescription, partyPK);
158                                    } else if(glAccountClassDescription != null && description != null) {
159                                        GlAccountClassDescriptionValue glAccountClassDescriptionValue = accountingControl.getGlAccountClassDescriptionValue(glAccountClassDescription);
160                                        
161                                        glAccountClassDescriptionValue.setDescription(description);
162                                        accountingControl.updateGlAccountClassDescriptionFromValue(glAccountClassDescriptionValue, partyPK);
163                                    }
164                                } finally {
165                                    unlockEntity(glAccountClass);
166                                }
167                            } else {
168                                addExecutionError(ExecutionErrors.EntityLockStale.name());
169                            }
170                        } else {
171                            addExecutionError(ExecutionErrors.InvalidParentGlAccountClass.name());
172                        }
173                    } else {
174                        addExecutionError(ExecutionErrors.UnknownParentGlAccountClassName.name(), parentGlAccountClassName);
175                    }
176                } else {
177                    addExecutionError(ExecutionErrors.DuplicateGlAccountClassName.name(), glAccountClassName);
178                }
179            } else {
180                addExecutionError(ExecutionErrors.UnknownGlAccountClassName.name(), glAccountClassName);
181            }
182            
183            if(hasExecutionErrors()) {
184                result.setGlAccountClass(accountingControl.getGlAccountClassTransfer(getUserVisit(), glAccountClass));
185                result.setEntityLock(getEntityLockTransfer(glAccountClass));
186            }
187        }
188        
189        return result;
190    }
191    
192}