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.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.item.server.entity.RelatedItemType;
032import com.echothree.model.data.item.server.value.RelatedItemDetailValue;
033import com.echothree.model.data.user.common.pk.UserVisitPK;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.common.command.EditMode;
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 EditRelatedItemCommand
048        extends BaseAbstractEditCommand<RelatedItemSpec, RelatedItemEdit, EditRelatedItemResult, RelatedItem, Item> {
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.RelatedItem.name(), SecurityRoles.Edit.name())
059                )))
060        )));
061
062        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
063                new FieldDefinition("RelatedItemTypeName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("FromItemName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("ToItemName", FieldType.ENTITY_NAME, true, null, null)
066                ));
067        
068        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
069                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
070                ));
071    }
072    
073    /** Creates a new instance of EditRelatedItemCommand */
074    public EditRelatedItemCommand(UserVisitPK userVisitPK, EditRelatedItemForm form) {
075        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077    
078    @Override
079    public EditRelatedItemResult getResult() {
080        return ItemResultFactory.getEditRelatedItemResult();
081    }
082    
083    @Override
084    public RelatedItemEdit getEdit() {
085        return ItemEditFactory.getRelatedItemEdit();
086    }
087    
088    @Override
089    public RelatedItem getEntity(EditRelatedItemResult result) {
090        var itemControl = Session.getModelController(ItemControl.class);
091        RelatedItem relatedItem = null;
092        String relatedItemTypeName = spec.getRelatedItemTypeName();
093        RelatedItemType relatedItemType = itemControl.getRelatedItemTypeByName(relatedItemTypeName);
094
095        if(relatedItemType != null) {
096            String fromItemName = spec.getFromItemName();
097            Item fromItem = itemControl.getItemByName(fromItemName);
098
099            if(fromItem != null) {
100                String toItemName = spec.getToItemName();
101                Item toItem = itemControl.getItemByName(toItemName);
102
103                if(toItem != null) {
104                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
105                        relatedItem = itemControl.getRelatedItem(relatedItemType, fromItem, toItem);
106                    } else { // EditMode.UPDATE
107                        relatedItem = itemControl.getRelatedItemForUpdate(relatedItemType, fromItem, toItem);
108                    }
109
110                    if(relatedItem == null) {
111                        addExecutionError(ExecutionErrors.UnknownRelatedItem.name(), relatedItemTypeName, fromItemName, toItemName);
112                    }
113                } else {
114                    addExecutionError(ExecutionErrors.UnknownToItemName.name(), toItemName);
115                }
116            } else {
117                addExecutionError(ExecutionErrors.UnknownFromItemName.name(), fromItemName);
118            }
119        } else {
120            addExecutionError(ExecutionErrors.UnknownRelatedItemTypeName.name(), relatedItemTypeName);
121        }
122
123        return relatedItem;
124    }
125    
126    @Override
127    public Item getLockEntity(RelatedItem relatedItem) {
128        return relatedItem.getLastDetail().getFromItem();
129    }
130    
131    @Override
132    public void fillInResult(EditRelatedItemResult result, RelatedItem relatedItem) {
133        var itemControl = Session.getModelController(ItemControl.class);
134        
135        result.setRelatedItem(itemControl.getRelatedItemTransfer(getUserVisit(), relatedItem));
136    }
137    
138    @Override
139    public void doLock(RelatedItemEdit edit, RelatedItem relatedItem) {
140        edit.setSortOrder(relatedItem.getLastDetail().getSortOrder().toString());
141    }
142
143    @Override
144    public void doUpdate(RelatedItem relatedItem) {
145        var itemControl = Session.getModelController(ItemControl.class);
146        RelatedItemDetailValue relatedItemValueDetail = itemControl.getRelatedItemDetailValueForUpdate(relatedItem);
147
148        relatedItemValueDetail.setSortOrder(Integer.valueOf(edit.getSortOrder()));
149
150        itemControl.updateRelatedItemFromValue(relatedItemValueDetail, getPartyPK());
151    }
152    
153}