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.track.server.command;
018
019import com.echothree.control.user.track.common.form.GetTrackForm;
020import com.echothree.control.user.track.common.result.GetTrackResult;
021import com.echothree.control.user.track.common.result.TrackResultFactory;
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.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.control.track.server.control.TrackControl;
030import com.echothree.model.data.core.server.entity.EntityInstance;
031import com.echothree.model.data.track.server.entity.Track;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.common.command.BaseResult;
037import com.echothree.util.server.control.BaseSimpleCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import com.echothree.util.server.persistence.Session;
042import java.util.Arrays;
043import java.util.Collections;
044import java.util.List;
045
046public class GetTrackCommand
047        extends BaseSimpleCommand<GetTrackForm> {
048    
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
056                        new SecurityRoleDefinition(SecurityRoleGroups.Track.name(), SecurityRoles.Review.name())
057                        )))
058                )));
059        
060        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
061                new FieldDefinition("TrackName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
063                new FieldDefinition("Key", FieldType.KEY, false, null, null),
064                new FieldDefinition("Guid", FieldType.GUID, false, null, null),
065                new FieldDefinition("Ulid", FieldType.ULID, false, null, null)
066                ));
067    }
068    
069    /** Creates a new instance of GetTrackCommand */
070    public GetTrackCommand(UserVisitPK userVisitPK, GetTrackForm form) {
071        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
072    }
073    
074    @Override
075    protected BaseResult execute() {
076        GetTrackResult result = TrackResultFactory.getGetTrackResult();
077        String trackName = form.getTrackName();
078        var parameterCount = (trackName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form);
079
080        if(parameterCount == 1) {
081            var trackControl = Session.getModelController(TrackControl.class);
082            Track track = null;
083
084            if(trackName == null) {
085                var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(),
086                        EntityTypes.Track.name());
087                
088                if(!hasExecutionErrors()) {
089                    track = trackControl.getTrackByEntityInstance(entityInstance);
090                }
091            } else {
092                track = trackControl.getTrackByName(trackName);
093
094                if(track == null) {
095                    addExecutionError(ExecutionErrors.UnknownTrackName.name(), trackName);
096                }
097            }
098
099            if(!hasExecutionErrors()) {
100                result.setTrack(trackControl.getTrackTransfer(getUserVisit(), track));
101                sendEvent(track.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
102            }
103        } else {
104            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
105        }
106        
107        return result;
108    }
109    
110}