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.GetFontWeightForm; 020import com.echothree.control.user.core.common.result.CoreResultFactory; 021import com.echothree.control.user.core.common.result.GetFontWeightResult; 022import com.echothree.model.control.core.common.ComponentVendors; 023import com.echothree.model.control.core.common.EntityTypes; 024import com.echothree.model.control.core.common.EventTypes; 025import com.echothree.model.control.core.server.logic.AppearanceLogic; 026import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 027import com.echothree.model.data.core.server.entity.EntityInstance; 028import com.echothree.model.data.core.server.entity.FontWeight; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 030import com.echothree.util.common.message.ExecutionErrors; 031import com.echothree.util.common.validation.FieldDefinition; 032import com.echothree.util.common.validation.FieldType; 033import com.echothree.util.common.command.BaseResult; 034import com.echothree.util.server.control.BaseSingleEntityCommand; 035import java.util.Arrays; 036import java.util.Collections; 037import java.util.List; 038 039public class GetFontWeightCommand 040 extends BaseSingleEntityCommand<FontWeight, GetFontWeightForm> { 041 042 // No COMMAND_SECURITY_DEFINITION, anyone may execute this command. 043 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 044 045 static { 046 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 047 new FieldDefinition("FontWeightName", FieldType.ENTITY_NAME, false, null, null), 048 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 049 new FieldDefinition("Key", FieldType.KEY, false, null, null), 050 new FieldDefinition("Guid", FieldType.GUID, false, null, null), 051 new FieldDefinition("Ulid", FieldType.ULID, false, null, null) 052 )); 053 } 054 055 /** Creates a new instance of GetFontWeightCommand */ 056 public GetFontWeightCommand(UserVisitPK userVisitPK, GetFontWeightForm form) { 057 super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true); 058 } 059 060 @Override 061 protected FontWeight getEntity() { 062 var coreControl = getCoreControl(); 063 FontWeight fontWeight = null; 064 String fontWeightName = form.getFontWeightName(); 065 var parameterCount = (fontWeightName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form); 066 067 if(parameterCount == 1) { 068 if(fontWeightName == null) { 069 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, 070 ComponentVendors.ECHO_THREE.name(), EntityTypes.FontWeight.name()); 071 072 if(!hasExecutionErrors()) { 073 fontWeight = coreControl.getFontWeightByEntityInstance(entityInstance); 074 } 075 } else { 076 fontWeight = AppearanceLogic.getInstance().getFontWeightByName(this, fontWeightName); 077 } 078 079 if(fontWeight != null) { 080 sendEvent(fontWeight.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK()); 081 } 082 } else { 083 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 084 } 085 086 return fontWeight; 087 } 088 089 @Override 090 protected BaseResult getResult(FontWeight fontWeight) { 091 var coreControl = getCoreControl(); 092 GetFontWeightResult result = CoreResultFactory.getGetFontWeightResult(); 093 094 if(fontWeight != null) { 095 result.setFontWeight(coreControl.getFontWeightTransfer(getUserVisit(), fontWeight)); 096 } 097 098 return result; 099 } 100 101}