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