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.content.server.command; 018 019import com.echothree.control.user.content.common.edit.ContentCategoryItemEdit; 020import com.echothree.control.user.content.common.edit.ContentEditFactory; 021import com.echothree.control.user.content.common.form.EditContentCategoryItemForm; 022import com.echothree.control.user.content.common.result.ContentResultFactory; 023import com.echothree.control.user.content.common.result.EditContentCategoryItemResult; 024import com.echothree.control.user.content.common.spec.ContentCategoryItemSpec; 025import com.echothree.model.control.accounting.server.control.AccountingControl; 026import com.echothree.model.control.content.server.control.ContentControl; 027import com.echothree.model.control.content.server.logic.ContentLogic; 028import com.echothree.model.control.inventory.server.control.InventoryControl; 029import com.echothree.model.control.item.server.control.ItemControl; 030import com.echothree.model.control.party.common.PartyTypes; 031import com.echothree.model.control.security.common.SecurityRoleGroups; 032import com.echothree.model.control.security.common.SecurityRoles; 033import com.echothree.model.control.uom.server.control.UomControl; 034import com.echothree.model.data.accounting.server.entity.Currency; 035import com.echothree.model.data.content.server.entity.ContentCatalog; 036import com.echothree.model.data.content.server.entity.ContentCatalogItem; 037import com.echothree.model.data.content.server.entity.ContentCategory; 038import com.echothree.model.data.content.server.entity.ContentCategoryItem; 039import com.echothree.model.data.content.server.entity.ContentCollection; 040import com.echothree.model.data.content.server.value.ContentCategoryItemValue; 041import com.echothree.model.data.inventory.server.entity.InventoryCondition; 042import com.echothree.model.data.item.server.entity.Item; 043import com.echothree.model.data.item.server.entity.ItemDetail; 044import com.echothree.model.data.uom.server.entity.UnitOfMeasureKind; 045import com.echothree.model.data.uom.server.entity.UnitOfMeasureType; 046import com.echothree.model.data.user.common.pk.UserVisitPK; 047import com.echothree.util.common.message.ExecutionErrors; 048import com.echothree.util.common.validation.FieldDefinition; 049import com.echothree.util.common.validation.FieldType; 050import com.echothree.util.common.command.EditMode; 051import com.echothree.util.server.control.BaseAbstractEditCommand; 052import com.echothree.util.server.control.CommandSecurityDefinition; 053import com.echothree.util.server.control.PartyTypeDefinition; 054import com.echothree.util.server.control.SecurityRoleDefinition; 055import com.echothree.util.server.persistence.Session; 056import java.util.Arrays; 057import java.util.Collections; 058import java.util.List; 059 060public class EditContentCategoryItemCommand 061 extends BaseAbstractEditCommand<ContentCategoryItemSpec, ContentCategoryItemEdit, EditContentCategoryItemResult, ContentCategoryItem, ContentCategory> { 062 063 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 064 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 065 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 066 067 static { 068 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 069 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 070 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 071 new SecurityRoleDefinition(SecurityRoleGroups.ContentCategoryItem.name(), SecurityRoles.Edit.name()) 072 ))) 073 ))); 074 075 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 076 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null), 077 new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null), 078 new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, true, null, null), 079 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null), 080 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null), 081 new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null), 082 new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null) 083 )); 084 085 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 086 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 087 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null) 088 )); 089 } 090 091 /** Creates a new instance of EditContentCategoryItemCommand */ 092 public EditContentCategoryItemCommand(UserVisitPK userVisitPK, EditContentCategoryItemForm form) { 093 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 094 } 095 096 @Override 097 public EditContentCategoryItemResult getResult() { 098 return ContentResultFactory.getEditContentCategoryItemResult(); 099 } 100 101 @Override 102 public ContentCategoryItemEdit getEdit() { 103 return ContentEditFactory.getContentCategoryItemEdit(); 104 } 105 106 @Override 107 public ContentCategoryItem getEntity(EditContentCategoryItemResult result) { 108 var contentControl = Session.getModelController(ContentControl.class); 109 ContentCategoryItem contentCategoryItem = null; 110 String contentCollectionName = spec.getContentCollectionName(); 111 ContentCollection contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 112 113 if(contentCollection != null) { 114 String contentCatalogName = spec.getContentCatalogName(); 115 ContentCatalog contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 116 117 if(contentCatalog != null) { 118 String contentCategoryName = spec.getContentCategoryName(); 119 ContentCategory contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName); 120 121 if(contentCategory != null) { 122 var itemControl = Session.getModelController(ItemControl.class); 123 String itemName = spec.getItemName(); 124 Item item = itemControl.getItemByName(itemName); 125 126 if(item != null) { 127 var inventoryControl = Session.getModelController(InventoryControl.class); 128 String inventoryConditionName = spec.getInventoryConditionName(); 129 InventoryCondition inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName); 130 131 if(inventoryCondition != null) { 132 var uomControl = Session.getModelController(UomControl.class); 133 String unitOfMeasureTypeName = spec.getUnitOfMeasureTypeName(); 134 ItemDetail itemDetail = item.getLastDetail(); 135 UnitOfMeasureKind unitOfMeasureKind = itemDetail.getUnitOfMeasureKind(); 136 UnitOfMeasureType unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName); 137 138 if(unitOfMeasureType != null) { 139 var accountingControl = Session.getModelController(AccountingControl.class); 140 String currencyIsoName = spec.getCurrencyIsoName(); 141 Currency currency = accountingControl.getCurrencyByIsoName(currencyIsoName); 142 143 if(currency != null) { 144 ContentCatalogItem contentCatalogItem = contentControl.getContentCatalogItem(contentCatalog, 145 item, inventoryCondition, unitOfMeasureType, currency); 146 147 if(contentCatalogItem != null) { 148 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 149 contentCategoryItem = contentControl.getContentCategoryItem(contentCategory, contentCatalogItem); 150 } else { // EditMode.UPDATE 151 contentCategoryItem = contentControl.getContentCategoryItemForUpdate(contentCategory, contentCatalogItem); 152 } 153 154 if(contentCategoryItem == null) { 155 addExecutionError(ExecutionErrors.UnknownContentCategoryItem.name()); 156 } 157 } else { 158 addExecutionError(ExecutionErrors.UnknownContentCatalogItem.name(), contentCollectionName, contentCatalogName, itemName, inventoryConditionName, 159 unitOfMeasureTypeName, currencyIsoName); 160 } 161 } else { 162 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName); 163 } 164 } else { 165 addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName); 166 } 167 } else { 168 addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName); 169 } 170 } else { 171 addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName); 172 } 173 } else { 174 addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(), contentCategoryName); 175 } 176 } else { 177 addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCatalogName); 178 } 179 } else { 180 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 181 } 182 183 return contentCategoryItem; 184 } 185 186 @Override 187 public ContentCategory getLockEntity(ContentCategoryItem contentCategoryItem) { 188 return contentCategoryItem.getContentCategory(); 189 } 190 191 @Override 192 public void fillInResult(EditContentCategoryItemResult result, ContentCategoryItem contentCategoryItem) { 193 var contentControl = Session.getModelController(ContentControl.class); 194 195 result.setContentCategoryItem(contentControl.getContentCategoryItemTransfer(getUserVisit(), contentCategoryItem)); 196 } 197 198 @Override 199 public void doLock(ContentCategoryItemEdit edit, ContentCategoryItem contentCategoryItem) { 200 edit.setIsDefault(contentCategoryItem.getIsDefault().toString()); 201 edit.setSortOrder(contentCategoryItem.getSortOrder().toString()); 202 } 203 204 @Override 205 public void doUpdate(ContentCategoryItem contentCategoryItem) { 206 var contentControl = Session.getModelController(ContentControl.class); 207 ContentCategoryItemValue contentCategoryItemValue = contentControl.getContentCategoryItemValue(contentCategoryItem); 208 209 contentCategoryItemValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 210 contentCategoryItemValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 211 212 ContentLogic.getInstance().updateContentCategoryItemFromValue(contentCategoryItemValue, getPartyPK()); 213 } 214 215}