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.item.server.command;
018
019import com.echothree.control.user.item.common.edit.ItemEditFactory;
020import com.echothree.control.user.item.common.edit.RelatedItemEdit;
021import com.echothree.control.user.item.common.form.EditRelatedItemForm;
022import com.echothree.control.user.item.common.result.EditRelatedItemResult;
023import com.echothree.control.user.item.common.result.ItemResultFactory;
024import com.echothree.control.user.item.common.spec.RelatedItemSpec;
025import com.echothree.model.control.item.server.control.ItemControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.item.server.entity.Item;
030import com.echothree.model.data.item.server.entity.RelatedItem;
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.common.command.EditMode;
036import com.echothree.util.server.control.BaseAbstractEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.List;
042import javax.enterprise.context.Dependent;
043
044@Dependent
045public class EditRelatedItemCommand
046        extends BaseAbstractEditCommand<RelatedItemSpec, RelatedItemEdit, EditRelatedItemResult, RelatedItem, Item> {
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(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.RelatedItem.name(), SecurityRoles.Edit.name())
057                ))
058        ));
059
060        SPEC_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("RelatedItemTypeName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("FromItemName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("ToItemName", FieldType.ENTITY_NAME, true, null, null)
064                );
065        
066        EDIT_FIELD_DEFINITIONS = List.of(
067                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
068                );
069    }
070    
071    /** Creates a new instance of EditRelatedItemCommand */
072    public EditRelatedItemCommand() {
073        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
074    }
075    
076    @Override
077    public EditRelatedItemResult getResult() {
078        return ItemResultFactory.getEditRelatedItemResult();
079    }
080    
081    @Override
082    public RelatedItemEdit getEdit() {
083        return ItemEditFactory.getRelatedItemEdit();
084    }
085    
086    @Override
087    public RelatedItem getEntity(EditRelatedItemResult result) {
088        var itemControl = Session.getModelController(ItemControl.class);
089        RelatedItem relatedItem = null;
090        var relatedItemTypeName = spec.getRelatedItemTypeName();
091        var relatedItemType = itemControl.getRelatedItemTypeByName(relatedItemTypeName);
092
093        if(relatedItemType != null) {
094            var fromItemName = spec.getFromItemName();
095            var fromItem = itemControl.getItemByName(fromItemName);
096
097            if(fromItem != null) {
098                var toItemName = spec.getToItemName();
099                var toItem = itemControl.getItemByName(toItemName);
100
101                if(toItem != null) {
102                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
103                        relatedItem = itemControl.getRelatedItem(relatedItemType, fromItem, toItem);
104                    } else { // EditMode.UPDATE
105                        relatedItem = itemControl.getRelatedItemForUpdate(relatedItemType, fromItem, toItem);
106                    }
107
108                    if(relatedItem == null) {
109                        addExecutionError(ExecutionErrors.UnknownRelatedItem.name(), relatedItemTypeName, fromItemName, toItemName);
110                    }
111                } else {
112                    addExecutionError(ExecutionErrors.UnknownToItemName.name(), toItemName);
113                }
114            } else {
115                addExecutionError(ExecutionErrors.UnknownFromItemName.name(), fromItemName);
116            }
117        } else {
118            addExecutionError(ExecutionErrors.UnknownRelatedItemTypeName.name(), relatedItemTypeName);
119        }
120
121        return relatedItem;
122    }
123    
124    @Override
125    public Item getLockEntity(RelatedItem relatedItem) {
126        return relatedItem.getLastDetail().getFromItem();
127    }
128    
129    @Override
130    public void fillInResult(EditRelatedItemResult result, RelatedItem relatedItem) {
131        var itemControl = Session.getModelController(ItemControl.class);
132        
133        result.setRelatedItem(itemControl.getRelatedItemTransfer(getUserVisit(), relatedItem));
134    }
135    
136    @Override
137    public void doLock(RelatedItemEdit edit, RelatedItem relatedItem) {
138        edit.setSortOrder(relatedItem.getLastDetail().getSortOrder().toString());
139    }
140
141    @Override
142    public void doUpdate(RelatedItem relatedItem) {
143        var itemControl = Session.getModelController(ItemControl.class);
144        var relatedItemValueDetail = itemControl.getRelatedItemDetailValueForUpdate(relatedItem);
145
146        relatedItemValueDetail.setSortOrder(Integer.valueOf(edit.getSortOrder()));
147
148        itemControl.updateRelatedItemFromValue(relatedItemValueDetail, getPartyPK());
149    }
150    
151}