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.TransactionGlAccountCategoryDescriptionEdit;
021import com.echothree.control.user.accounting.common.form.EditTransactionGlAccountCategoryDescriptionForm;
022import com.echothree.control.user.accounting.common.result.AccountingResultFactory;
023import com.echothree.control.user.accounting.common.result.EditTransactionGlAccountCategoryDescriptionResult;
024import com.echothree.control.user.accounting.common.spec.TransactionGlAccountCategoryDescriptionSpec;
025import com.echothree.model.control.accounting.server.control.AccountingControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.party.server.control.PartyControl;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.accounting.server.entity.TransactionGlAccountCategory;
031import com.echothree.model.data.accounting.server.entity.TransactionGlAccountCategoryDescription;
032import com.echothree.model.data.accounting.server.entity.TransactionType;
033import com.echothree.model.data.accounting.server.value.TransactionGlAccountCategoryDescriptionValue;
034import com.echothree.model.data.party.server.entity.Language;
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 EditTransactionGlAccountCategoryDescriptionCommand
051        extends BaseEditCommand<TransactionGlAccountCategoryDescriptionSpec, TransactionGlAccountCategoryDescriptionEdit> {
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.TransactionGlAccountCategory.name(), SecurityRoles.Description.name())
062                        )))
063                )));
064        
065        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
066                new FieldDefinition("TransactionTypeName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("TransactionGlAccountCategoryName", FieldType.ENTITY_NAME, true, null, null),
068                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
069                ));
070        
071        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
072                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
073                ));
074    }
075    
076    /** Creates a new instance of EditTransactionGlAccountCategoryDescriptionCommand */
077    public EditTransactionGlAccountCategoryDescriptionCommand(UserVisitPK userVisitPK, EditTransactionGlAccountCategoryDescriptionForm form) {
078        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
079    }
080    
081    @Override
082    protected BaseResult execute() {
083        var accountingControl = Session.getModelController(AccountingControl.class);
084        EditTransactionGlAccountCategoryDescriptionResult result = AccountingResultFactory.getEditTransactionGlAccountCategoryDescriptionResult();
085        String transactionTypeName = spec.getTransactionTypeName();
086        TransactionType transactionType = accountingControl.getTransactionTypeByName(transactionTypeName);
087        
088        if(transactionType != null) {
089            String transactionGlAccountCategoryName = spec.getTransactionGlAccountCategoryName();
090            TransactionGlAccountCategory transactionGlAccountCategory = accountingControl.getTransactionGlAccountCategoryByName(transactionType, transactionGlAccountCategoryName);
091
092            if(transactionGlAccountCategory != null) {
093                var partyControl = Session.getModelController(PartyControl.class);
094                String languageIsoName = spec.getLanguageIsoName();
095                Language language = partyControl.getLanguageByIsoName(languageIsoName);
096
097                if(language != null) {
098                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
099                        TransactionGlAccountCategoryDescription transactionGlAccountCategoryDescription = accountingControl.getTransactionGlAccountCategoryDescription(transactionGlAccountCategory, language);
100
101                        if(transactionGlAccountCategoryDescription != null) {
102                            if(editMode.equals(EditMode.LOCK)) {
103                                result.setTransactionGlAccountCategoryDescription(accountingControl.getTransactionGlAccountCategoryDescriptionTransfer(getUserVisit(), transactionGlAccountCategoryDescription));
104
105                                if(lockEntity(transactionGlAccountCategory)) {
106                                    TransactionGlAccountCategoryDescriptionEdit edit = AccountingEditFactory.getTransactionGlAccountCategoryDescriptionEdit();
107
108                                    result.setEdit(edit);
109                                    edit.setDescription(transactionGlAccountCategoryDescription.getDescription());
110                                } else {
111                                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
112                                }
113
114                                result.setEntityLock(getEntityLockTransfer(transactionGlAccountCategory));
115                            } else { // EditMode.ABANDON
116                                unlockEntity(transactionGlAccountCategory);
117                            }
118                        } else {
119                            addExecutionError(ExecutionErrors.UnknownTransactionGlAccountCategoryDescription.name());
120                        }
121                    } else if(editMode.equals(EditMode.UPDATE)) {
122                        TransactionGlAccountCategoryDescriptionValue transactionGlAccountCategoryDescriptionValue = accountingControl.getTransactionGlAccountCategoryDescriptionValueForUpdate(transactionGlAccountCategory, language);
123
124                        if(transactionGlAccountCategoryDescriptionValue != null) {
125                            if(lockEntityForUpdate(transactionGlAccountCategory)) {
126                                try {
127                                    String description = edit.getDescription();
128
129                                    transactionGlAccountCategoryDescriptionValue.setDescription(description);
130
131                                    accountingControl.updateTransactionGlAccountCategoryDescriptionFromValue(transactionGlAccountCategoryDescriptionValue, getPartyPK());
132                                } finally {
133                                    unlockEntity(transactionGlAccountCategory);
134                                }
135                            } else {
136                                addExecutionError(ExecutionErrors.EntityLockStale.name());
137                            }
138                        } else {
139                            addExecutionError(ExecutionErrors.UnknownTransactionGlAccountCategoryDescription.name());
140                        }
141                    }
142                } else {
143                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
144                }
145            } else {
146                addExecutionError(ExecutionErrors.UnknownTransactionGlAccountCategoryName.name(), transactionGlAccountCategoryName);
147            }
148        } else {
149            addExecutionError(ExecutionErrors.UnknownTransactionTypeName.name(), transactionTypeName);
150        }
151        
152        return result;
153    }
154    
155}