001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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.GetApplicationForm; 020import com.echothree.control.user.core.common.result.CoreResultFactory; 021import com.echothree.model.control.core.common.ComponentVendors; 022import com.echothree.model.control.core.common.EntityTypes; 023import com.echothree.model.control.core.common.EventTypes; 024import com.echothree.model.control.core.server.control.ApplicationControl; 025import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 026import com.echothree.model.data.core.server.entity.Application; 027import com.echothree.model.data.user.common.pk.UserVisitPK; 028import com.echothree.util.common.command.BaseResult; 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.server.control.BaseSimpleCommand; 033import java.util.List; 034import javax.enterprise.context.Dependent; 035import javax.inject.Inject; 036 037@Dependent 038public class GetApplicationCommand 039 extends BaseSimpleCommand<GetApplicationForm> { 040 041 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 042 043 static { 044 FORM_FIELD_DEFINITIONS = List.of( 045 new FieldDefinition("ApplicationName", FieldType.ENTITY_NAME, false, null, null), 046 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 047 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 048 ); 049 } 050 051 @Inject 052 ApplicationControl applicationControl; 053 054 @Inject 055 EntityInstanceLogic entityInstanceLogic; 056 057 058 /** Creates a new instance of GetApplicationCommand */ 059 public GetApplicationCommand() { 060 super(null, FORM_FIELD_DEFINITIONS, true); 061 } 062 063 @Override 064 protected BaseResult execute() { 065 var result = CoreResultFactory.getGetApplicationResult(); 066 var applicationName = form.getApplicationName(); 067 var parameterCount = (applicationName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(form); 068 069 if(parameterCount == 1) { 070 Application application = null; 071 072 if(applicationName == null) { 073 var entityInstance = entityInstanceLogic.getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(), 074 EntityTypes.Application.name()); 075 076 if(!hasExecutionErrors()) { 077 application = applicationControl.getApplicationByEntityInstance(entityInstance); 078 } 079 } else { 080 application = applicationControl.getApplicationByName(applicationName); 081 082 if(application == null) { 083 addExecutionError(ExecutionErrors.UnknownApplicationName.name(), applicationName); 084 } 085 } 086 087 if(!hasExecutionErrors()) { 088 result.setApplication(applicationControl.getApplicationTransfer(getUserVisit(), application)); 089 sendEvent(application.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK()); 090 } 091 } else { 092 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 093 } 094 095 return result; 096 } 097 098}