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.form.CreateContentCategoryItemForm; 020import com.echothree.model.control.accounting.server.control.AccountingControl; 021import com.echothree.model.control.content.server.control.ContentControl; 022import com.echothree.model.control.content.server.logic.ContentLogic; 023import com.echothree.model.control.inventory.server.control.InventoryControl; 024import com.echothree.model.control.item.server.control.ItemControl; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.security.common.SecurityRoleGroups; 027import com.echothree.model.control.security.common.SecurityRoles; 028import com.echothree.model.control.uom.server.control.UomControl; 029import com.echothree.model.data.accounting.server.entity.Currency; 030import com.echothree.model.data.content.server.entity.ContentCatalog; 031import com.echothree.model.data.content.server.entity.ContentCategory; 032import com.echothree.model.data.content.server.entity.ContentCollection; 033import com.echothree.model.data.inventory.server.entity.InventoryCondition; 034import com.echothree.model.data.item.server.entity.Item; 035import com.echothree.model.data.item.server.entity.ItemDetail; 036import com.echothree.model.data.uom.server.entity.UnitOfMeasureKind; 037import com.echothree.model.data.uom.server.entity.UnitOfMeasureType; 038import com.echothree.model.data.user.common.pk.UserVisitPK; 039import com.echothree.util.common.message.ExecutionErrors; 040import com.echothree.util.common.validation.FieldDefinition; 041import com.echothree.util.common.validation.FieldType; 042import com.echothree.util.common.command.BaseResult; 043import com.echothree.util.server.control.BaseSimpleCommand; 044import com.echothree.util.server.control.CommandSecurityDefinition; 045import com.echothree.util.server.control.PartyTypeDefinition; 046import com.echothree.util.server.control.SecurityRoleDefinition; 047import com.echothree.util.server.persistence.Session; 048import java.util.Arrays; 049import java.util.Collections; 050import java.util.List; 051 052public class CreateContentCategoryItemCommand 053 extends BaseSimpleCommand<CreateContentCategoryItemForm> { 054 055 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 056 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 057 058 static { 059 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 060 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 061 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 062 new SecurityRoleDefinition(SecurityRoleGroups.ContentCategoryItem.name(), SecurityRoles.Create.name()) 063 ))) 064 ))); 065 066 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 067 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null), 068 new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, true, null, null), 070 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null), 071 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null), 072 new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null), 073 new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null), 074 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 075 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null) 076 )); 077 } 078 079 /** Creates a new instance of CreateContentCategoryItemCommand */ 080 public CreateContentCategoryItemCommand(UserVisitPK userVisitPK, CreateContentCategoryItemForm form) { 081 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 082 } 083 084 @Override 085 protected BaseResult execute() { 086 var contentControl = Session.getModelController(ContentControl.class); 087 String contentCollectionName = form.getContentCollectionName(); 088 ContentCollection contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 089 090 if(contentCollection != null) { 091 String contentCatalogName = form.getContentCatalogName(); 092 ContentCatalog contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 093 094 if(contentCatalog != null) { 095 String contentCategoryName = form.getContentCategoryName(); 096 ContentCategory contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName); 097 098 if(contentCategory != null) { 099 var itemControl = Session.getModelController(ItemControl.class); 100 String itemName = form.getItemName(); 101 Item item = itemControl.getItemByName(itemName); 102 103 if(item != null) { 104 var inventoryControl = Session.getModelController(InventoryControl.class); 105 String inventoryConditionName = form.getInventoryConditionName(); 106 InventoryCondition inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName); 107 108 if(inventoryCondition != null) { 109 var uomControl = Session.getModelController(UomControl.class); 110 String unitOfMeasureTypeName = form.getUnitOfMeasureTypeName(); 111 ItemDetail itemDetail = item.getLastDetail(); 112 UnitOfMeasureKind unitOfMeasureKind = itemDetail.getUnitOfMeasureKind(); 113 UnitOfMeasureType unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName); 114 115 if(unitOfMeasureType != null) { 116 var accountingControl = Session.getModelController(AccountingControl.class); 117 String currencyIsoName = form.getCurrencyIsoName(); 118 Currency currency = accountingControl.getCurrencyByIsoName(currencyIsoName); 119 120 if(currency != null) { 121 var isDefault = Boolean.valueOf(form.getIsDefault()); 122 var sortOrder = Integer.valueOf(form.getSortOrder()); 123 124 ContentLogic.getInstance().createContentCategoryItem(this, contentCategory, item, inventoryCondition, unitOfMeasureType, 125 currency, isDefault, sortOrder, getPartyPK()); 126 } else { 127 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName); 128 } 129 } else { 130 addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName(), 131 unitOfMeasureTypeName); 132 } 133 } else { 134 addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName); 135 } 136 } else { 137 addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName); 138 } 139 } else { 140 addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(), contentCollectionName, contentCatalogName, contentCategoryName); 141 } 142 } else { 143 addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCollectionName, contentCatalogName); 144 } 145 } else { 146 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 147 } 148 149 return null; 150 } 151 152}