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.GlAccountEdit;
021import com.echothree.control.user.accounting.common.form.EditGlAccountForm;
022import com.echothree.control.user.accounting.common.result.AccountingResultFactory;
023import com.echothree.control.user.accounting.common.spec.GlAccountUniversalSpec;
024import com.echothree.model.control.accounting.server.control.AccountingControl;
025import com.echothree.model.control.accounting.server.logic.GlAccountLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.accounting.server.entity.GlAccount;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.command.EditMode;
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.server.control.BaseEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.List;
042import javax.enterprise.context.Dependent;
043
044@Dependent
045public class EditGlAccountCommand
046        extends BaseEditCommand<GlAccountUniversalSpec, GlAccountEdit> {
047    
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
050    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                new SecurityRoleDefinition(SecurityRoleGroups.GlAccount.name(), SecurityRoles.Edit.name())
057                ))
058                ));
059        
060        SPEC_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("GlAccountName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
063                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
064                );
065        
066        EDIT_FIELD_DEFINITIONS = List.of(
067                new FieldDefinition("GlAccountName", FieldType.ENTITY_NAME, true, null, null),
068                new FieldDefinition("ParentGlAccountName", FieldType.ENTITY_NAME, false, null, null),
069                new FieldDefinition("GlAccountClassName", FieldType.ENTITY_NAME, true, null, null),
070                new FieldDefinition("GlAccountCategoryName", FieldType.ENTITY_NAME, false, null, null),
071                new FieldDefinition("GlResourceTypeName", FieldType.ENTITY_NAME, true, null, null),
072                new FieldDefinition("IsDefault", FieldType.BOOLEAN, false, null, null),
073                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
074                );
075    }
076    
077    /** Creates a new instance of EditGlAccountCommand */
078    public EditGlAccountCommand() {
079        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081    
082    @Override
083    protected BaseResult execute() {
084        var accountingControl = Session.getModelController(AccountingControl.class);
085        var result = AccountingResultFactory.getEditGlAccountResult();
086        
087        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
088            var glAccount = GlAccountLogic.getInstance().getGlAccountByUniversalSpec(this, spec, false, null);
089
090            if(!hasExecutionErrors()) {
091                if(editMode.equals(EditMode.LOCK)) {
092                    if(lockEntity(glAccount)) {
093                        var glAccountDescription = accountingControl.getGlAccountDescription(glAccount, getPreferredLanguage());
094                        var edit = AccountingEditFactory.getGlAccountEdit();
095                        var glAccountDetail = glAccount.getLastDetail();
096                        var parentGlAccount = glAccountDetail.getParentGlAccount();
097                        var glAccountCategory = glAccountDetail.getGlAccountCategory();
098                        var isDefault = glAccountDetail.getIsDefault();
099
100                        result.setGlAccount(accountingControl.getGlAccountTransfer(getUserVisit(), glAccount));
101
102                        result.setEdit(edit);
103                        edit.setGlAccountName(glAccountDetail.getGlAccountName());
104                        edit.setParentGlAccountName(parentGlAccount == null? null: parentGlAccount.getLastDetail().getGlAccountName());
105                        edit.setGlAccountClassName(glAccountDetail.getGlAccountClass().getLastDetail().getGlAccountClassName());
106                        edit.setGlAccountCategoryName(glAccountCategory == null? null: glAccountCategory.getLastDetail().getGlAccountCategoryName());
107                        edit.setGlResourceTypeName(glAccountDetail.getGlResourceType().getLastDetail().getGlResourceTypeName());
108                        edit.setIsDefault(isDefault == null? null: isDefault.toString());
109
110                        if(glAccountDescription != null) {
111                            edit.setDescription(glAccountDescription.getDescription());
112                        }
113                    } else {
114                        addExecutionError(ExecutionErrors.EntityLockFailed.name());
115                    }
116
117                    result.setEntityLock(getEntityLockTransfer(glAccount));
118                } else { // EditMode.ABANDON
119                    unlockEntity(glAccount);
120                }
121            }
122        } else if(editMode.equals(EditMode.UPDATE)) {
123            var glAccount = GlAccountLogic.getInstance().getGlAccountByUniversalSpecForUpdate(this, spec, false, null);
124
125            if(!hasExecutionErrors()) {
126                var glAccountName = edit.getGlAccountName();
127                var duplicateGlAccount = accountingControl.getGlAccountByName(glAccountName);
128                
129                if(duplicateGlAccount == null || glAccount.equals(duplicateGlAccount)) {
130                    var parentGlAccountName = edit.getParentGlAccountName();
131                    GlAccount parentGlAccount = null;
132                    
133                    if(parentGlAccountName != null) {
134                        parentGlAccount = accountingControl.getGlAccountByName(parentGlAccountName);
135                    }
136                    
137                    if(parentGlAccountName == null || parentGlAccount != null) {
138                        if(accountingControl.isParentGlAccountSafe(glAccount, parentGlAccount)) {
139                            var glAccountClassName = edit.getGlAccountClassName();
140                            var glAccountClass = accountingControl.getGlAccountClassByName(glAccountClassName);
141                            
142                            if(glAccountClass != null) {
143                                var glAccountCategoryName = edit.getGlAccountCategoryName();
144                                var glAccountCategory = glAccountCategoryName == null? null: accountingControl.getGlAccountCategoryByName(glAccountCategoryName);
145                                
146                                if(glAccountCategoryName == null || glAccountCategory != null) {
147                                    var glResourceTypeName = edit.getGlResourceTypeName();
148                                    var glResourceType = accountingControl.getGlResourceTypeByName(glResourceTypeName);
149                                    
150                                    if(glResourceType != null) {
151                                        if(lockEntityForUpdate(glAccount)) {
152                                            try {
153                                                var partyPK = getPartyPK();
154                                                var glAccountDetailValue = accountingControl.getGlAccountDetailValueForUpdate(glAccount);
155                                                var glAccountDescription = accountingControl.getGlAccountDescriptionForUpdate(glAccount, getPreferredLanguage());
156                                                var isDefault = glAccountCategory == null? null: Boolean.valueOf(edit.getIsDefault());
157                                                var description = edit.getDescription();
158                                                
159                                                glAccountDetailValue.setGlAccountName(glAccountName);
160                                                glAccountDetailValue.setParentGlAccountPK(parentGlAccount == null? null: parentGlAccount.getPrimaryKey());
161                                                glAccountDetailValue.setGlAccountClassPK(glAccountClass.getPrimaryKey());
162                                                glAccountDetailValue.setGlAccountCategoryPK(glAccountCategory == null? null: glAccountCategory.getPrimaryKey());
163                                                glAccountDetailValue.setGlResourceTypePK(glResourceType.getPrimaryKey());
164                                                glAccountDetailValue.setIsDefault(isDefault);
165                                                
166                                                GlAccountLogic.getInstance().updateGlAccountFromValue(glAccountDetailValue, partyPK);
167                                                
168                                                if(glAccountDescription == null && description != null) {
169                                                    accountingControl.createGlAccountDescription(glAccount, getPreferredLanguage(), description, partyPK);
170                                                } else if(glAccountDescription != null && description == null) {
171                                                    accountingControl.deleteGlAccountDescription(glAccountDescription, partyPK);
172                                                } else if(glAccountDescription != null && description != null) {
173                                                    var glAccountDescriptionValue = accountingControl.getGlAccountDescriptionValue(glAccountDescription);
174                                                    
175                                                    glAccountDescriptionValue.setDescription(description);
176                                                    accountingControl.updateGlAccountDescriptionFromValue(glAccountDescriptionValue, partyPK);
177                                                }
178                                            } finally {
179                                                unlockEntity(glAccount);
180                                            }
181                                        } else {
182                                            addExecutionError(ExecutionErrors.EntityLockStale.name());
183                                        }
184                                    } else {
185                                        addExecutionError(ExecutionErrors.UnknownGlResourceTypeName.name(), glResourceTypeName);
186                                    }
187                                } else {
188                                    addExecutionError(ExecutionErrors.UnknownGlAccountCategoryName.name(), glAccountCategoryName);
189                                }
190                            } else {
191                                addExecutionError(ExecutionErrors.UnknownGlAccountClassName.name(), glAccountClassName);
192                            }
193                        } else {
194                            addExecutionError(ExecutionErrors.InvalidParentGlAccount.name());
195                        }
196                    } else {
197                        addExecutionError(ExecutionErrors.UnknownParentGlAccountName.name(), parentGlAccountName);
198                    }
199                } else {
200                    addExecutionError(ExecutionErrors.DuplicateGlAccountName.name(), glAccountName);
201                }
202            }
203            
204            if(hasExecutionErrors()) {
205                result.setGlAccount(accountingControl.getGlAccountTransfer(getUserVisit(), glAccount));
206                result.setEntityLock(getEntityLockTransfer(glAccount));
207            }
208        }
209        
210        return result;
211    }
212    
213}