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