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.ApplicationLogic;
026import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
027import com.echothree.model.data.core.server.entity.Application;
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.BaseSingleEntityCommand;
033import java.util.List;
034import javax.enterprise.context.Dependent;
035import javax.inject.Inject;
036
037@Dependent
038public class GetApplicationCommand
039        extends BaseSingleEntityCommand<Application, 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    ApplicationLogic applicationLogic;
056
057    @Inject
058    EntityInstanceLogic entityInstanceLogic;
059
060    /** Creates a new instance of GetApplicationCommand */
061    public GetApplicationCommand() {
062        super(null, FORM_FIELD_DEFINITIONS, true);
063    }
064    
065    @Override
066    protected Application getEntity() {
067        var applicationName = form.getApplicationName();
068        var parameterCount = (applicationName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(form);
069        Application application = null;
070
071        if(parameterCount == 1) {
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 = applicationLogic.getApplicationByName(this, applicationName);
081            }
082
083            if(application != null) {
084                sendEvent(application.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
085            }
086        } else {
087            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
088        }
089
090        return application;
091    }
092
093    @Override
094    protected BaseResult getResult(Application application) {
095        var result = CoreResultFactory.getGetApplicationResult();
096
097        if(application != null) {
098            result.setApplication(applicationControl.getApplicationTransfer(getUserVisit(), application));
099        }
100
101        return result;
102    }
103    
104}