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.form.CreateRelatedItemForm;
020import com.echothree.control.user.item.common.result.ItemResultFactory;
021import com.echothree.model.control.item.server.control.ItemControl;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.security.common.SecurityRoleGroups;
024import com.echothree.model.control.security.common.SecurityRoles;
025import com.echothree.model.data.user.common.pk.UserVisitPK;
026import com.echothree.util.common.command.BaseResult;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.server.control.BaseSimpleCommand;
031import com.echothree.util.server.control.CommandSecurityDefinition;
032import com.echothree.util.server.control.PartyTypeDefinition;
033import com.echothree.util.server.control.SecurityRoleDefinition;
034import com.echothree.util.server.persistence.Session;
035import java.util.Arrays;
036import java.util.Collections;
037import java.util.List;
038
039public class CreateRelatedItemCommand
040        extends BaseSimpleCommand<CreateRelatedItemForm> {
041
042    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
043    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
044    
045    static {
046        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
047                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
048                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
049                        new SecurityRoleDefinition(SecurityRoleGroups.RelatedItem.name(), SecurityRoles.Create.name())
050                )))
051        )));
052
053        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
054            new FieldDefinition("RelatedItemTypeName", FieldType.ENTITY_NAME, true, null, null),
055            new FieldDefinition("FromItemName", FieldType.ENTITY_NAME, true, null, null),
056            new FieldDefinition("ToItemName", FieldType.ENTITY_NAME, true, null, null),
057            new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
058        ));
059    }
060    
061    /** Creates a new instance of CreateRelatedItemCommand */
062    public CreateRelatedItemCommand(UserVisitPK userVisitPK, CreateRelatedItemForm form) {
063        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
064    }
065    
066    @Override
067    protected BaseResult execute() {
068        var result = ItemResultFactory.getCreateRelatedItemResult();
069        var itemControl = Session.getModelController(ItemControl.class);
070        var relatedItemTypeName = form.getRelatedItemTypeName();
071        var relatedItemType = itemControl.getRelatedItemTypeByName(relatedItemTypeName);
072        
073        if(relatedItemType != null) {
074            var fromItemName = form.getFromItemName();
075            var fromItem = itemControl.getItemByName(fromItemName);
076            
077            if(fromItem != null) {
078                var toItemName = form.getToItemName();
079                var toItem = itemControl.getItemByName(toItemName);
080                
081                if(toItem != null) {
082                    if(!fromItem.equals(toItem)) {
083                        var relatedItem = itemControl.getRelatedItem(relatedItemType, fromItem, toItem);
084
085                        if(relatedItem == null) {
086                            var sortOrder = Integer.valueOf(form.getSortOrder());
087
088                            itemControl.createRelatedItem(relatedItemType, fromItem, toItem, sortOrder, getPartyPK());
089
090                            result.setRelatedItemTypeName(relatedItemType.getLastDetail().getRelatedItemTypeName());
091                            result.setFromItemName(fromItem.getLastDetail().getItemName());
092                            result.setToItemName(toItem.getLastDetail().getItemName());
093                            result.setEntityRef(relatedItemType.getPrimaryKey().getEntityRef());
094                        } else {
095                            addExecutionError(ExecutionErrors.DuplicateRelatedItem.name(),
096                                    relatedItemType.getLastDetail().getRelatedItemTypeName(),
097                                    fromItem.getLastDetail().getItemName(), toItem.getLastDetail().getItemName());
098                        }
099                    } else {
100                        addExecutionError(ExecutionErrors.CannotRelateToSelf.name(),
101                                fromItem.getLastDetail().getItemName(), toItem.getLastDetail().getItemName());
102                    }
103                } else {
104                    addExecutionError(ExecutionErrors.UnknownToItemName.name(), toItemName);
105                }
106            } else {
107                addExecutionError(ExecutionErrors.UnknownFromItemName.name(), fromItemName);
108            }
109        } else {
110            addExecutionError(ExecutionErrors.UnknownRelatedItemTypeName.name(), relatedItemTypeName);
111        }
112
113        return result;
114    }
115    
116}