001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.TransactionGlAccountCategoryEdit;
021import com.echothree.control.user.accounting.common.form.EditTransactionGlAccountCategoryForm;
022import com.echothree.control.user.accounting.common.result.AccountingResultFactory;
023import com.echothree.control.user.accounting.common.spec.TransactionGlAccountCategorySpec;
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.user.common.pk.UserVisitPK;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.common.command.EditMode;
034import com.echothree.util.server.control.BaseEditCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import com.echothree.util.server.persistence.Session;
039import java.util.Arrays;
040import java.util.Collections;
041import java.util.List;
042import javax.enterprise.context.RequestScoped;
043
044@RequestScoped
045public class EditTransactionGlAccountCategoryCommand
046        extends BaseEditCommand<TransactionGlAccountCategorySpec, TransactionGlAccountCategoryEdit> {
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(Collections.unmodifiableList(Arrays.asList(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
056                        new SecurityRoleDefinition(SecurityRoleGroups.TransactionGlAccountCategory.name(), SecurityRoles.Edit.name())
057                        )))
058                )));
059        
060        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
061                new FieldDefinition("TransactionTypeName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("TransactionGlAccountCategoryName", FieldType.ENTITY_NAME, true, null, null)
063                ));
064        
065        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
066                new FieldDefinition("TransactionGlAccountCategoryName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("GlAccountCategoryName", FieldType.ENTITY_NAME, false, null, null),
068                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
069                new FieldDefinition("GlAccountName", FieldType.ENTITY_NAME, false, null, null),
070                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
071                ));
072    }
073    
074    /** Creates a new instance of EditTransactionGlAccountCategoryCommand */
075    public EditTransactionGlAccountCategoryCommand() {
076        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
077    }
078    
079    @Override
080    protected BaseResult execute() {
081        var accountingControl = Session.getModelController(AccountingControl.class);
082        var result = AccountingResultFactory.getEditTransactionGlAccountCategoryResult();
083        var transactionTypeName = spec.getTransactionTypeName();
084        var transactionType = accountingControl.getTransactionTypeByNameForUpdate(transactionTypeName);
085        
086        if(transactionType != null) {
087            var transactionGlAccountCategoryName = spec.getTransactionGlAccountCategoryName();
088
089            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
090                var transactionGlAccountCategory = accountingControl.getTransactionGlAccountCategoryByName(transactionType, transactionGlAccountCategoryName);
091
092                if(transactionGlAccountCategory != null) {
093                    if(editMode.equals(EditMode.LOCK)) {
094                        if(lockEntity(transactionGlAccountCategory)) {
095                            var transactionGlAccountCategoryDescription = accountingControl.getTransactionGlAccountCategoryDescription(transactionGlAccountCategory, getPreferredLanguage());
096                            var transactionGlAccount = accountingControl.getTransactionGlAccount(transactionGlAccountCategory);
097                            var edit = AccountingEditFactory.getTransactionGlAccountCategoryEdit();
098                            var transactionGlAccountCategoryDetail = transactionGlAccountCategory.getLastDetail();
099                            var glAccountCategory = transactionGlAccountCategoryDetail.getGlAccountCategory();
100
101                            result.setTransactionGlAccountCategory(accountingControl.getTransactionGlAccountCategoryTransfer(getUserVisit(), transactionGlAccountCategory));
102
103                            result.setEdit(edit);
104                            edit.setTransactionGlAccountCategoryName(transactionGlAccountCategoryDetail.getTransactionGlAccountCategoryName());
105                            edit.setGlAccountCategoryName(glAccountCategory == null? null: glAccountCategory.getLastDetail().getGlAccountCategoryName());
106                            edit.setSortOrder(transactionGlAccountCategoryDetail.getSortOrder().toString());
107                            edit.setGlAccountName(transactionGlAccount == null? null: transactionGlAccount.getGlAccount().getLastDetail().getGlAccountName());
108
109                            if(transactionGlAccountCategoryDescription != null) {
110                                edit.setDescription(transactionGlAccountCategoryDescription.getDescription());
111                            }
112                        } else {
113                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
114                        }
115
116                        result.setEntityLock(getEntityLockTransfer(transactionGlAccountCategory));
117                    } else { // EditMode.ABANDON
118                        unlockEntity(transactionGlAccountCategory);
119                    }
120                } else {
121                    addExecutionError(ExecutionErrors.UnknownTransactionGlAccountCategoryName.name(), transactionTypeName, transactionGlAccountCategoryName);
122                }
123            } else if(editMode.equals(EditMode.UPDATE)) {
124                var transactionGlAccountCategory = accountingControl.getTransactionGlAccountCategoryByNameForUpdate(transactionType, transactionGlAccountCategoryName);
125
126                if(transactionGlAccountCategory != null) {
127                    transactionGlAccountCategoryName = edit.getTransactionGlAccountCategoryName();
128                    var duplicateTransactionGlAccountCategory = accountingControl.getTransactionGlAccountCategoryByName(transactionType, transactionGlAccountCategoryName);
129
130                    if(duplicateTransactionGlAccountCategory == null || transactionGlAccountCategory.equals(duplicateTransactionGlAccountCategory)) {
131                        var glAccountCategoryName = edit.getGlAccountCategoryName();
132                        var glAccountCategory = glAccountCategoryName == null? null: accountingControl.getGlAccountCategoryByName(glAccountCategoryName);
133
134                        if(glAccountCategoryName == null || glAccountCategory != null) {
135                            var glAccountName = edit.getGlAccountName();
136                            var glAccount = glAccountName == null? null: accountingControl.getGlAccountByName(glAccountName);
137
138                            if(glAccountName == null || glAccount != null) {
139                                if(glAccountCategory == null || glAccount == null? true: glAccountCategory.equals(glAccount.getLastDetail().getGlAccountCategory())) {
140                                    if(lockEntityForUpdate(transactionGlAccountCategory)) {
141                                        try {
142                                            var partyPK = getPartyPK();
143                                            var transactionGlAccountCategoryDetailValue = accountingControl.getTransactionGlAccountCategoryDetailValueForUpdate(transactionGlAccountCategory);
144                                            var transactionGlAccountCategoryDescription = accountingControl.getTransactionGlAccountCategoryDescriptionForUpdate(transactionGlAccountCategory, getPreferredLanguage());
145                                            var transactionGlAccount = accountingControl.getTransactionGlAccount(transactionGlAccountCategory);
146                                            var description = edit.getDescription();
147
148                                            transactionGlAccountCategoryDetailValue.setTransactionGlAccountCategoryName(edit.getTransactionGlAccountCategoryName());
149                                            transactionGlAccountCategoryDetailValue.setGlAccountCategoryPK(glAccountCategory == null? null: glAccountCategory.getPrimaryKey());
150                                            transactionGlAccountCategoryDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
151
152                                            accountingControl.updateTransactionGlAccountCategoryFromValue(transactionGlAccountCategoryDetailValue, partyPK);
153
154                                            if(transactionGlAccountCategoryDescription == null && description != null) {
155                                                accountingControl.createTransactionGlAccountCategoryDescription(transactionGlAccountCategory, getPreferredLanguage(), description, partyPK);
156                                            } else if(transactionGlAccountCategoryDescription != null && description == null) {
157                                                accountingControl.deleteTransactionGlAccountCategoryDescription(transactionGlAccountCategoryDescription, partyPK);
158                                            } else if(transactionGlAccountCategoryDescription != null && description != null) {
159                                                var transactionGlAccountCategoryDescriptionValue = accountingControl.getTransactionGlAccountCategoryDescriptionValue(transactionGlAccountCategoryDescription);
160
161                                                transactionGlAccountCategoryDescriptionValue.setDescription(description);
162                                                accountingControl.updateTransactionGlAccountCategoryDescriptionFromValue(transactionGlAccountCategoryDescriptionValue, partyPK);
163                                            }
164
165                                            if(transactionGlAccount == null && glAccount != null) {
166                                                accountingControl.createTransactionGlAccount(transactionGlAccountCategory, glAccount, partyPK);
167                                            } else if(transactionGlAccount != null && glAccount == null) {
168                                                accountingControl.deleteTransactionGlAccount(transactionGlAccount, partyPK);
169                                            } else if(transactionGlAccount != null && glAccount != null) {
170                                                var transactionGlAccountValue = accountingControl.getTransactionGlAccountValue(transactionGlAccount);
171
172                                                transactionGlAccountValue.setGlAccountPK(glAccount.getPrimaryKey());
173                                                accountingControl.updateTransactionGlAccountFromValue(transactionGlAccountValue, partyPK);
174                                            }
175                                        } finally {
176                                            unlockEntity(transactionGlAccountCategory);
177                                        }
178                                    } else {
179                                        addExecutionError(ExecutionErrors.EntityLockStale.name());
180                                    }
181                                } else {
182                                    addExecutionError(ExecutionErrors.InvalidGlAccountCategory.name());
183                                }
184                            } else {
185                                addExecutionError(ExecutionErrors.UnknownGlAccountName.name(), glAccountName);
186                            }
187                        } else {
188                            addExecutionError(ExecutionErrors.UnknownGlAccountCategoryName.name(), glAccountCategoryName);
189                        }
190                    } else {
191                        addExecutionError(ExecutionErrors.DuplicateTransactionGlAccountCategoryName.name(), transactionTypeName, transactionGlAccountCategoryName);
192                    }
193
194                    if(hasExecutionErrors()) {
195                        result.setTransactionGlAccountCategory(accountingControl.getTransactionGlAccountCategoryTransfer(getUserVisit(), transactionGlAccountCategory));
196                        result.setEntityLock(getEntityLockTransfer(transactionGlAccountCategory));
197                    }
198                } else {
199                    addExecutionError(ExecutionErrors.UnknownTransactionGlAccountCategoryName.name(), transactionTypeName, transactionGlAccountCategoryName);
200                }
201            }
202        } else {
203            addExecutionError(ExecutionErrors.UnknownTransactionTypeName.name(), transactionTypeName);
204        }
205        
206        return result;
207    }
208    
209}