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.party.server.command;
018
019import com.echothree.control.user.party.common.form.GetDateTimeFormatForm;
020import com.echothree.control.user.party.common.result.GetDateTimeFormatResult;
021import com.echothree.control.user.party.common.result.PartyResultFactory;
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.server.control.PartyControl;
027import com.echothree.model.control.party.server.logic.DateTimeFormatLogic;
028import com.echothree.model.data.core.server.entity.EntityInstance;
029import com.echothree.model.data.party.server.entity.DateTimeFormat;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.common.command.BaseResult;
035import com.echothree.util.server.control.BaseSingleEntityCommand;
036import com.echothree.util.server.persistence.Session;
037import java.util.Arrays;
038import java.util.Collections;
039import java.util.List;
040
041public class GetDateTimeFormatCommand
042        extends BaseSingleEntityCommand<DateTimeFormat, GetDateTimeFormatForm> {
043    
044    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
045    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
046    
047    static {
048        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
049                new FieldDefinition("DateTimeFormatName", FieldType.ENTITY_NAME, false, null, null),
050                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
051                new FieldDefinition("Key", FieldType.KEY, false, null, null),
052                new FieldDefinition("Guid", FieldType.GUID, false, null, null),
053                new FieldDefinition("Ulid", FieldType.ULID, false, null, null)
054                ));
055    }
056    
057    /** Creates a new instance of GetDateTimeFormatCommand */
058    public GetDateTimeFormatCommand(UserVisitPK userVisitPK, GetDateTimeFormatForm form) {
059        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
060    }
061    
062    @Override
063    protected DateTimeFormat getEntity() {
064        var partyControl = Session.getModelController(PartyControl.class);
065        DateTimeFormat dateTimeFormat = null;
066        String dateTimeFormatName = form.getDateTimeFormatName();
067        var parameterCount = (dateTimeFormatName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form);
068
069        switch(parameterCount) {
070            case 0:
071                dateTimeFormat = partyControl.getDefaultDateTimeFormat();
072                break;
073            case 1:
074                if(dateTimeFormatName == null) {
075                    var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form,
076                            ComponentVendors.ECHO_THREE.name(), EntityTypes.DateTimeFormat.name());
077                    
078                    if(!hasExecutionErrors()) {
079                        dateTimeFormat = partyControl.getDateTimeFormatByEntityInstance(entityInstance);
080                    }
081                } else {
082                    dateTimeFormat = DateTimeFormatLogic.getInstance().getDateTimeFormatByName(this, dateTimeFormatName);
083                }
084                break;
085            default:
086                addExecutionError(ExecutionErrors.InvalidParameterCount.name());
087                break;
088        }
089        
090        if(dateTimeFormat != null) {
091            sendEvent(dateTimeFormat.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
092        }
093
094        return dateTimeFormat;
095    }
096    
097    @Override
098    protected BaseResult getResult(DateTimeFormat dateTimeFormat) {
099        var partyControl = Session.getModelController(PartyControl.class);
100        GetDateTimeFormatResult result = PartyResultFactory.getGetDateTimeFormatResult();
101
102        if(dateTimeFormat != null) {
103            result.setDateTimeFormat(partyControl.getDateTimeFormatTransfer(getUserVisit(), dateTimeFormat));
104        }
105
106        return result;
107    }
108    
109}