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.GlAccountCategoryEdit;
021import com.echothree.control.user.accounting.common.form.EditGlAccountCategoryForm;
022import com.echothree.control.user.accounting.common.result.AccountingResultFactory;
023import com.echothree.control.user.accounting.common.result.EditGlAccountCategoryResult;
024import com.echothree.control.user.accounting.common.spec.GlAccountCategorySpec;
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.GlAccountCategory;
031import com.echothree.model.data.accounting.server.entity.GlAccountCategoryDescription;
032import com.echothree.model.data.accounting.server.entity.GlAccountCategoryDetail;
033import com.echothree.model.data.accounting.server.value.GlAccountCategoryDescriptionValue;
034import com.echothree.model.data.accounting.server.value.GlAccountCategoryDetailValue;
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 EditGlAccountCategoryCommand
051        extends BaseEditCommand<GlAccountCategorySpec, GlAccountCategoryEdit> {
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.GlAccountCategory.name(), SecurityRoles.Edit.name())
062                    )))
063                )));
064        
065        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
066                new FieldDefinition("GlAccountCategoryName", FieldType.ENTITY_NAME, true, null, null)
067                ));
068        
069        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
070                new FieldDefinition("GlAccountCategoryName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("ParentGlAccountCategoryName", 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 EditGlAccountCategoryCommand */
079    public EditGlAccountCategoryCommand(UserVisitPK userVisitPK, EditGlAccountCategoryForm 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        EditGlAccountCategoryResult result = AccountingResultFactory.getEditGlAccountCategoryResult();
087        
088        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
089            String glAccountCategoryName = spec.getGlAccountCategoryName();
090            GlAccountCategory glAccountCategory = accountingControl.getGlAccountCategoryByName(glAccountCategoryName);
091            
092            if(glAccountCategory != null) {
093                if(editMode.equals(EditMode.LOCK)) {
094                    if(lockEntity(glAccountCategory)) {
095                        GlAccountCategoryDescription glAccountCategoryDescription = accountingControl.getGlAccountCategoryDescription(glAccountCategory, getPreferredLanguage());
096                        GlAccountCategoryEdit edit = AccountingEditFactory.getGlAccountCategoryEdit();
097                        GlAccountCategoryDetail glAccountCategoryDetail = glAccountCategory.getLastDetail();
098                        GlAccountCategory parentGlAccountCategory = glAccountCategoryDetail.getParentGlAccountCategory();
099
100                        result.setGlAccountCategory(accountingControl.getGlAccountCategoryTransfer(getUserVisit(), glAccountCategory));
101
102                        result.setEdit(edit);
103                        edit.setGlAccountCategoryName(glAccountCategoryDetail.getGlAccountCategoryName());
104                        edit.setParentGlAccountCategoryName(parentGlAccountCategory == null? null: parentGlAccountCategory.getLastDetail().getGlAccountCategoryName());
105                        edit.setIsDefault(glAccountCategoryDetail.getIsDefault().toString());
106                        edit.setSortOrder(glAccountCategoryDetail.getSortOrder().toString());
107
108                        if(glAccountCategoryDescription != null) {
109                            edit.setDescription(glAccountCategoryDescription.getDescription());
110                        }
111                    } else {
112                        addExecutionError(ExecutionErrors.EntityLockFailed.name());
113                    }
114
115                    result.setEntityLock(getEntityLockTransfer(glAccountCategory));
116                } else { // EditMode.ABANDON
117                    unlockEntity(glAccountCategory);
118                }
119            } else {
120                addExecutionError(ExecutionErrors.UnknownGlAccountCategoryName.name(), glAccountCategoryName);
121            }
122        } else if(editMode.equals(EditMode.UPDATE)) {
123            String glAccountCategoryName = spec.getGlAccountCategoryName();
124            GlAccountCategory glAccountCategory = accountingControl.getGlAccountCategoryByNameForUpdate(glAccountCategoryName);
125            
126            if(glAccountCategory != null) {
127                glAccountCategoryName = edit.getGlAccountCategoryName();
128                GlAccountCategory duplicateGlAccountCategory = accountingControl.getGlAccountCategoryByName(glAccountCategoryName);
129                
130                if(duplicateGlAccountCategory == null || glAccountCategory.equals(duplicateGlAccountCategory)) {
131                    String parentGlAccountCategoryName = edit.getParentGlAccountCategoryName();
132                    GlAccountCategory parentGlAccountCategory = null;
133                    
134                    if(parentGlAccountCategoryName != null) {
135                        parentGlAccountCategory = accountingControl.getGlAccountCategoryByName(parentGlAccountCategoryName);
136                    }
137                    
138                    if(parentGlAccountCategoryName == null || parentGlAccountCategory != null) {
139                        if(accountingControl.isParentGlAccountCategorySafe(glAccountCategory, parentGlAccountCategory)) {
140                            if(lockEntityForUpdate(glAccountCategory)) {
141                                try {
142                                    var partyPK = getPartyPK();
143                                    GlAccountCategoryDetailValue glAccountCategoryDetailValue = accountingControl.getGlAccountCategoryDetailValueForUpdate(glAccountCategory);
144                                    GlAccountCategoryDescription glAccountCategoryDescription = accountingControl.getGlAccountCategoryDescriptionForUpdate(glAccountCategory, getPreferredLanguage());
145                                    String description = edit.getDescription();
146                                    
147                                    glAccountCategoryDetailValue.setGlAccountCategoryName(edit.getGlAccountCategoryName());
148                                    glAccountCategoryDetailValue.setParentGlAccountCategoryPK(parentGlAccountCategory == null? null: parentGlAccountCategory.getPrimaryKey());
149                                    glAccountCategoryDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
150                                    glAccountCategoryDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
151                                    
152                                    accountingControl.updateGlAccountCategoryFromValue(glAccountCategoryDetailValue, partyPK);
153                                    
154                                    if(glAccountCategoryDescription == null && description != null) {
155                                        accountingControl.createGlAccountCategoryDescription(glAccountCategory, getPreferredLanguage(), description, partyPK);
156                                    } else if(glAccountCategoryDescription != null && description == null) {
157                                        accountingControl.deleteGlAccountCategoryDescription(glAccountCategoryDescription, partyPK);
158                                    } else if(glAccountCategoryDescription != null && description != null) {
159                                        GlAccountCategoryDescriptionValue glAccountCategoryDescriptionValue = accountingControl.getGlAccountCategoryDescriptionValue(glAccountCategoryDescription);
160                                        
161                                        glAccountCategoryDescriptionValue.setDescription(description);
162                                        accountingControl.updateGlAccountCategoryDescriptionFromValue(glAccountCategoryDescriptionValue, partyPK);
163                                    }
164                                } finally {
165                                    unlockEntity(glAccountCategory);
166                                }
167                            } else {
168                                addExecutionError(ExecutionErrors.EntityLockStale.name());
169                            }
170                        } else {
171                            addExecutionError(ExecutionErrors.InvalidParentGlAccountCategory.name());
172                        }
173                    } else {
174                        addExecutionError(ExecutionErrors.UnknownParentGlAccountCategoryName.name(), parentGlAccountCategoryName);
175                    }
176                } else {
177                    addExecutionError(ExecutionErrors.DuplicateGlAccountCategoryName.name(), glAccountCategoryName);
178                }
179            } else {
180                addExecutionError(ExecutionErrors.UnknownGlAccountCategoryName.name(), glAccountCategoryName);
181            }
182            
183            if(hasExecutionErrors()) {
184                result.setGlAccountCategory(accountingControl.getGlAccountCategoryTransfer(getUserVisit(), glAccountCategory));
185                result.setEntityLock(getEntityLockTransfer(glAccountCategory));
186            }
187        }
188        
189        return result;
190    }
191    
192}