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.core.server.command; 018 019import com.echothree.control.user.core.common.form.CreateEntityInstanceForm; 020import com.echothree.control.user.core.common.form.CreateEntityTypeForm; 021import com.echothree.control.user.core.common.result.CoreResultFactory; 022import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 023import com.echothree.model.control.core.server.logic.EntityTypeLogic; 024import com.echothree.model.control.party.common.PartyTypes; 025import com.echothree.model.control.security.common.SecurityRoleGroups; 026import com.echothree.model.control.security.common.SecurityRoles; 027import com.echothree.model.control.uom.common.UomConstants; 028import com.echothree.model.control.uom.server.logic.UnitOfMeasureTypeLogic; 029import com.echothree.model.data.core.server.entity.EntityInstance; 030import com.echothree.model.data.core.server.entity.EntityType; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.common.persistence.BasePK; 035import com.echothree.util.common.validation.FieldDefinition; 036import com.echothree.util.common.validation.FieldType; 037import com.echothree.util.server.control.BaseSimpleCommand; 038import com.echothree.util.server.control.CommandSecurityDefinition; 039import com.echothree.util.server.control.PartyTypeDefinition; 040import com.echothree.util.server.control.SecurityRoleDefinition; 041import com.echothree.util.server.persistence.PersistenceUtils; 042import java.util.Arrays; 043import java.util.Collections; 044import java.util.List; 045 046public class CreateEntityInstanceCommand 047 extends BaseSimpleCommand<CreateEntityInstanceForm> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> FORM_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.EntityInstance.name(), SecurityRoles.Create.name()) 057 )) 058 )); 059 060 FORM_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null) 063 ); 064 } 065 066 /** Creates a new instance of CreateEntityTypeCommand */ 067 public CreateEntityInstanceCommand(UserVisitPK userVisitPK, CreateEntityInstanceForm form) { 068 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 069 } 070 071 @Override 072 protected BaseResult execute() { 073 var result = CoreResultFactory.getCreateEntityInstanceResult(); 074 var entityType = EntityTypeLogic.getInstance().getEntityTypeByName(this, form.getComponentVendorName(), form.getEntityTypeName()); 075 EntityInstance entityInstance = null; 076 077 if(!hasExecutionErrors()) { 078 entityInstance = EntityInstanceLogic.getInstance().createEntityInstance(this, entityType, getPartyPK()); 079 } 080 081 if(entityInstance != null) { 082 var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance); 083 084 result.setEntityRef(basePK.getEntityRef()); 085 } 086 087 return result; 088 } 089 090}