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.GetApplicationForm;
020import com.echothree.control.user.core.common.result.CoreResultFactory;
021import com.echothree.control.user.core.common.result.GetApplicationResult;
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.EntityInstanceLogic;
026import com.echothree.model.data.core.server.entity.Application;
027import com.echothree.model.data.core.server.entity.EntityInstance;
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 java.util.Arrays;
035import java.util.Collections;
036import java.util.List;
037
038public class GetApplicationCommand
039        extends BaseSimpleCommand<GetApplicationForm> {
040    
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042    
043    static {
044        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
045                new FieldDefinition("ApplicationName", FieldType.ENTITY_NAME, false, null, null),
046                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
047                new FieldDefinition("Key", FieldType.KEY, false, null, null),
048                new FieldDefinition("Guid", FieldType.GUID, false, null, null),
049                new FieldDefinition("Ulid", FieldType.ULID, false, null, null)
050                ));
051    }
052    
053    /** Creates a new instance of GetApplicationCommand */
054    public GetApplicationCommand(UserVisitPK userVisitPK, GetApplicationForm form) {
055        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
056    }
057    
058    @Override
059    protected BaseResult execute() {
060        GetApplicationResult result = CoreResultFactory.getGetApplicationResult();
061        String applicationName = form.getApplicationName();
062        var parameterCount = (applicationName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form);
063
064        if(parameterCount == 1) {
065            var coreControl = getCoreControl();
066            Application application = null;
067
068            if(applicationName == null) {
069                var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(),
070                        EntityTypes.Application.name());
071                
072                if(!hasExecutionErrors()) {
073                    application = coreControl.getApplicationByEntityInstance(entityInstance);
074                }
075            } else {
076                application = coreControl.getApplicationByName(applicationName);
077
078                if(application == null) {
079                    addExecutionError(ExecutionErrors.UnknownApplicationName.name(), applicationName);
080                }
081            }
082
083            if(!hasExecutionErrors()) {
084                result.setApplication(coreControl.getApplicationTransfer(getUserVisit(), application));
085                sendEvent(application.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
086            }
087        } else {
088            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
089        }
090        
091        return result;
092    }
093    
094}