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