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 java.util.List; 041import javax.enterprise.context.Dependent; 042import javax.inject.Inject; 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 @Inject 072 ItemControl itemControl; 073 074 075 /** Creates a new instance of EditRelatedItemCommand */ 076 public EditRelatedItemCommand() { 077 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 public EditRelatedItemResult getResult() { 082 return ItemResultFactory.getEditRelatedItemResult(); 083 } 084 085 @Override 086 public RelatedItemEdit getEdit() { 087 return ItemEditFactory.getRelatedItemEdit(); 088 } 089 090 @Override 091 public RelatedItem getEntity(EditRelatedItemResult result) { 092 RelatedItem relatedItem = null; 093 var relatedItemTypeName = spec.getRelatedItemTypeName(); 094 var relatedItemType = itemControl.getRelatedItemTypeByName(relatedItemTypeName); 095 096 if(relatedItemType != null) { 097 var fromItemName = spec.getFromItemName(); 098 var fromItem = itemControl.getItemByName(fromItemName); 099 100 if(fromItem != null) { 101 var toItemName = spec.getToItemName(); 102 var toItem = itemControl.getItemByName(toItemName); 103 104 if(toItem != null) { 105 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 106 relatedItem = itemControl.getRelatedItem(relatedItemType, fromItem, toItem); 107 } else { // EditMode.UPDATE 108 relatedItem = itemControl.getRelatedItemForUpdate(relatedItemType, fromItem, toItem); 109 } 110 111 if(relatedItem == null) { 112 addExecutionError(ExecutionErrors.UnknownRelatedItem.name(), relatedItemTypeName, fromItemName, toItemName); 113 } 114 } else { 115 addExecutionError(ExecutionErrors.UnknownToItemName.name(), toItemName); 116 } 117 } else { 118 addExecutionError(ExecutionErrors.UnknownFromItemName.name(), fromItemName); 119 } 120 } else { 121 addExecutionError(ExecutionErrors.UnknownRelatedItemTypeName.name(), relatedItemTypeName); 122 } 123 124 return relatedItem; 125 } 126 127 @Override 128 public Item getLockEntity(RelatedItem relatedItem) { 129 return relatedItem.getLastDetail().getFromItem(); 130 } 131 132 @Override 133 public void fillInResult(EditRelatedItemResult result, RelatedItem relatedItem) { 134 result.setRelatedItem(itemControl.getRelatedItemTransfer(getUserVisit(), relatedItem)); 135 } 136 137 @Override 138 public void doLock(RelatedItemEdit edit, RelatedItem relatedItem) { 139 edit.setSortOrder(relatedItem.getLastDetail().getSortOrder().toString()); 140 } 141 142 @Override 143 public void doUpdate(RelatedItem relatedItem) { 144 var relatedItemValueDetail = itemControl.getRelatedItemDetailValueForUpdate(relatedItem); 145 146 relatedItemValueDetail.setSortOrder(Integer.valueOf(edit.getSortOrder())); 147 148 itemControl.updateRelatedItemFromValue(relatedItemValueDetail, getPartyPK()); 149 } 150 151}