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