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.ItemAccountingCategoryEdit;
021import com.echothree.control.user.accounting.common.form.EditItemAccountingCategoryForm;
022import com.echothree.control.user.accounting.common.result.AccountingResultFactory;
023import com.echothree.control.user.accounting.common.result.EditItemAccountingCategoryResult;
024import com.echothree.control.user.accounting.common.spec.ItemAccountingCategoryUniversalSpec;
025import com.echothree.model.control.accounting.server.control.AccountingControl;
026import com.echothree.model.control.accounting.server.logic.ItemAccountingCategoryLogic;
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.ItemAccountingCategory;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseAbstractEditCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class EditItemAccountingCategoryCommand
045        extends BaseAbstractEditCommand<ItemAccountingCategoryUniversalSpec, ItemAccountingCategoryEdit, EditItemAccountingCategoryResult, ItemAccountingCategory, ItemAccountingCategory> {
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.ItemAccountingCategory.name(), SecurityRoles.Edit.name())
056                ))
057        ));
058        
059        SPEC_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("ItemAccountingCategoryName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
062                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
063        );
064        
065        EDIT_FIELD_DEFINITIONS = List.of(
066                new FieldDefinition("ItemAccountingCategoryName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("ParentItemAccountingCategoryName", FieldType.ENTITY_NAME, false, null, null),
068                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
069                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
070                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
071        );
072    }
073
074    @Inject
075    AccountingControl accountingControl;
076
077    @Inject
078    ItemAccountingCategoryLogic itemAccountingCategoryLogic;
079
080    /** Creates a new instance of EditItemAccountingCategoryCommand */
081    public EditItemAccountingCategoryCommand() {
082        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
083    }
084
085    @Override
086    public EditItemAccountingCategoryResult getResult() {
087        return AccountingResultFactory.getEditItemAccountingCategoryResult();
088    }
089
090    @Override
091    public ItemAccountingCategoryEdit getEdit() {
092        return AccountingEditFactory.getItemAccountingCategoryEdit();
093    }
094
095    @Override
096    public ItemAccountingCategory getEntity(EditItemAccountingCategoryResult result) {
097        return itemAccountingCategoryLogic.getItemAccountingCategoryByUniversalSpec(this, spec, false, editModeToEntityPermission(editMode));
098    }
099
100    @Override
101    public ItemAccountingCategory getLockEntity(ItemAccountingCategory itemAccountingCategory) {
102        return itemAccountingCategory;
103    }
104
105    @Override
106    public void fillInResult(EditItemAccountingCategoryResult result, ItemAccountingCategory itemAccountingCategory) {
107        result.setItemAccountingCategory(accountingControl.getItemAccountingCategoryTransfer(getUserVisit(), itemAccountingCategory));
108    }
109
110    ItemAccountingCategory parentItemAccountingCategory = null;
111
112    @Override
113    public void doLock(ItemAccountingCategoryEdit edit, ItemAccountingCategory itemAccountingCategory) {
114        var itemAccountingCategoryDescription = accountingControl.getItemAccountingCategoryDescription(itemAccountingCategory, getPreferredLanguage());
115        var itemAccountingCategoryDetail = itemAccountingCategory.getLastDetail();
116
117        parentItemAccountingCategory = itemAccountingCategoryDetail.getParentItemAccountingCategory();
118
119        edit.setItemAccountingCategoryName(itemAccountingCategoryDetail.getItemAccountingCategoryName());
120        edit.setParentItemAccountingCategoryName(parentItemAccountingCategory == null? null: parentItemAccountingCategory.getLastDetail().getItemAccountingCategoryName());
121        edit.setIsDefault(itemAccountingCategoryDetail.getIsDefault().toString());
122        edit.setSortOrder(itemAccountingCategoryDetail.getSortOrder().toString());
123
124        if(itemAccountingCategoryDescription != null) {
125            edit.setDescription(itemAccountingCategoryDescription.getDescription());
126        }
127    }
128
129    @Override
130    public void canUpdate(ItemAccountingCategory itemAccountingCategory) {
131        var itemAccountingCategoryName = edit.getItemAccountingCategoryName();
132        var duplicateItemAccountingCategory = accountingControl.getItemAccountingCategoryByName(itemAccountingCategoryName);
133
134        if(duplicateItemAccountingCategory == null || itemAccountingCategory.equals(duplicateItemAccountingCategory)) {
135            var parentItemAccountingCategoryName = edit.getParentItemAccountingCategoryName();
136
137            parentItemAccountingCategory = parentItemAccountingCategoryName == null? null: accountingControl.getItemAccountingCategoryByName(parentItemAccountingCategoryName);
138
139            if(parentItemAccountingCategoryName == null || parentItemAccountingCategory != null) {
140                if(parentItemAccountingCategory != null) {
141                    if(!accountingControl.isParentItemAccountingCategorySafe(itemAccountingCategory, parentItemAccountingCategory)) {
142                        addExecutionError(ExecutionErrors.InvalidParentItemAccountingCategory.name());
143                    }
144                }
145            } else {
146                addExecutionError(ExecutionErrors.UnknownParentItemAccountingCategoryName.name(), parentItemAccountingCategoryName);
147            }
148        } else {
149            addExecutionError(ExecutionErrors.DuplicateItemAccountingCategoryName.name(), itemAccountingCategoryName);
150        }
151    }
152
153    @Override
154    public void doUpdate(ItemAccountingCategory itemAccountingCategory) {
155        var partyPK = getPartyPK();
156        var itemAccountingCategoryDetailValue = accountingControl.getItemAccountingCategoryDetailValueForUpdate(itemAccountingCategory);
157        var itemAccountingCategoryDescription = accountingControl.getItemAccountingCategoryDescriptionForUpdate(itemAccountingCategory, getPreferredLanguage());
158        var description = edit.getDescription();
159
160        itemAccountingCategoryDetailValue.setItemAccountingCategoryName(edit.getItemAccountingCategoryName());
161        itemAccountingCategoryDetailValue.setParentItemAccountingCategoryPK(parentItemAccountingCategory == null? null: parentItemAccountingCategory.getPrimaryKey());
162        itemAccountingCategoryDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
163        itemAccountingCategoryDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
164
165        itemAccountingCategoryLogic.updateItemAccountingCategoryFromValue(itemAccountingCategoryDetailValue, partyPK);
166
167        if(itemAccountingCategoryDescription == null && description != null) {
168            accountingControl.createItemAccountingCategoryDescription(itemAccountingCategory, getPreferredLanguage(), description, partyPK);
169        } else if(itemAccountingCategoryDescription != null && description == null) {
170            accountingControl.deleteItemAccountingCategoryDescription(itemAccountingCategoryDescription, partyPK);
171        } else if(itemAccountingCategoryDescription != null && description != null) {
172            var itemAccountingCategoryDescriptionValue = accountingControl.getItemAccountingCategoryDescriptionValue(itemAccountingCategoryDescription);
173
174            itemAccountingCategoryDescriptionValue.setDescription(description);
175            accountingControl.updateItemAccountingCategoryDescriptionFromValue(itemAccountingCategoryDescriptionValue, partyPK);
176        }
177    }
178    
179}