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.DeleteEntityStringAttributeForm; 020import com.echothree.model.control.core.server.logic.EntityAttributeLogic; 021import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 022import com.echothree.model.control.party.common.PartyTypes; 023import com.echothree.model.control.party.server.logic.LanguageLogic; 024import com.echothree.model.data.core.server.entity.EntityAttribute; 025import com.echothree.model.data.core.server.entity.EntityInstance; 026import com.echothree.model.data.core.server.entity.EntityStringAttribute; 027import com.echothree.model.data.party.server.entity.Language; 028import com.echothree.model.data.user.common.pk.UserVisitPK; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.server.control.BaseSimpleCommand; 034import com.echothree.util.server.control.CommandSecurityDefinition; 035import com.echothree.util.server.control.PartyTypeDefinition; 036import java.util.Arrays; 037import java.util.Collections; 038import java.util.List; 039 040public class DeleteEntityStringAttributeCommand 041 extends BaseSimpleCommand<DeleteEntityStringAttributeForm> { 042 043 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 044 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 045 046 static { 047 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 048 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 049 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null) 050 )); 051 052 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 053 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 054 new FieldDefinition("Key", FieldType.KEY, false, null, null), 055 new FieldDefinition("Guid", FieldType.GUID, false, null, null), 056 new FieldDefinition("Ulid", FieldType.ULID, false, null, null), 057 new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null), 058 new FieldDefinition("EntityAttributeUlid", FieldType.ULID, false, null, null), 059 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("LanguageUlid", FieldType.ENTITY_NAME, false, null, null) 061 )); 062 } 063 064 /** Creates a new instance of DeleteEntityStringAttributeCommand */ 065 public DeleteEntityStringAttributeCommand(UserVisitPK userVisitPK, DeleteEntityStringAttributeForm form) { 066 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 067 } 068 069 @Override 070 protected BaseResult execute() { 071 var parameterCount = EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form); 072 073 if(parameterCount == 1) { 074 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form); 075 076 if(!hasExecutionErrors()) { 077 String entityAttributeName = form.getEntityAttributeName(); 078 String entityAttributeUlid = form.getEntityAttributeUlid(); 079 080 parameterCount = (entityAttributeName == null ? 0 : 1) + (entityAttributeUlid == null ? 0 : 1); 081 082 if(parameterCount == 1) { 083 EntityAttribute entityAttribute = entityAttributeName == null ? 084 EntityAttributeLogic.getInstance().getEntityAttributeByUlid(this, entityAttributeUlid) : 085 EntityAttributeLogic.getInstance().getEntityAttributeByName(this, entityInstance.getEntityType(), entityAttributeName); 086 087 if(!hasExecutionErrors()) { 088 if(entityInstance.getEntityType().equals(entityAttribute.getLastDetail().getEntityType())) { 089 String languageIsoName = form.getLanguageIsoName(); 090 String languageUlid = form.getLanguageUlid(); 091 092 parameterCount = (languageIsoName == null ? 0 : 1) + (languageUlid == null ? 0 : 1); 093 094 if(parameterCount == 1) { 095 Language language = languageIsoName == null ? 096 LanguageLogic.getInstance().getLanguageByUlid(this, languageUlid) : 097 LanguageLogic.getInstance().getLanguageByName(this, languageIsoName); 098 099 if(!hasExecutionErrors()) { 100 var coreControl = getCoreControl(); 101 EntityStringAttribute entityStringAttribute = coreControl.getEntityStringAttributeForUpdate(entityAttribute, entityInstance, language); 102 103 if(entityStringAttribute != null) { 104 coreControl.deleteEntityStringAttribute(entityStringAttribute, getPartyPK()); 105 } else { 106 addExecutionError(ExecutionErrors.UnknownEntityStringAttribute.name()); 107 } 108 } 109 } else { 110 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 111 } 112 } else { 113 addExecutionError(ExecutionErrors.MismatchedEntityType.name()); 114 } 115 } 116 } else { 117 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 118 } 119 } 120 } else { 121 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 122 } 123 124 return null; 125 } 126 127}