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 com.echothree.util.server.persistence.Session;
034import java.util.List;
035import javax.enterprise.context.Dependent;
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    /** Creates a new instance of GetApplicationCommand */
052    public GetApplicationCommand() {
053        super(null, FORM_FIELD_DEFINITIONS, true);
054    }
055    
056    @Override
057    protected BaseResult execute() {
058        var result = CoreResultFactory.getGetApplicationResult();
059        var applicationName = form.getApplicationName();
060        var parameterCount = (applicationName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form);
061
062        if(parameterCount == 1) {
063            var applicationControl = Session.getModelController(ApplicationControl.class);
064            Application application = null;
065
066            if(applicationName == null) {
067                var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(),
068                        EntityTypes.Application.name());
069                
070                if(!hasExecutionErrors()) {
071                    application = applicationControl.getApplicationByEntityInstance(entityInstance);
072                }
073            } else {
074                application = applicationControl.getApplicationByName(applicationName);
075
076                if(application == null) {
077                    addExecutionError(ExecutionErrors.UnknownApplicationName.name(), applicationName);
078                }
079            }
080
081            if(!hasExecutionErrors()) {
082                result.setApplication(applicationControl.getApplicationTransfer(getUserVisit(), application));
083                sendEvent(application.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
084            }
085        } else {
086            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
087        }
088        
089        return result;
090    }
091    
092}