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.CreateDateTimeFormatForm;
020import com.echothree.control.user.party.common.result.PartyResultFactory;
021import com.echothree.model.control.party.server.control.PartyControl;
022import com.echothree.model.data.user.common.pk.UserVisitPK;
023import com.echothree.util.common.command.BaseResult;
024import com.echothree.util.common.message.ExecutionErrors;
025import com.echothree.util.common.validation.FieldDefinition;
026import com.echothree.util.common.validation.FieldType;
027import com.echothree.util.server.control.BaseSimpleCommand;
028import java.util.List;
029import javax.enterprise.context.Dependent;
030import javax.inject.Inject;
031
032@Dependent
033public class CreateDateTimeFormatCommand
034        extends BaseSimpleCommand<CreateDateTimeFormatForm> {
035
036    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
037
038    static {
039        FORM_FIELD_DEFINITIONS = List.of(
040                new FieldDefinition("DateTimeFormatName", FieldType.ENTITY_NAME, true, null, null),
041                new FieldDefinition("JavaShortDateFormat", FieldType.STRING, true, 1L, 60L),
042                new FieldDefinition("JavaAbbrevDateFormat", FieldType.STRING, true, 1L, 60L),
043                new FieldDefinition("JavaAbbrevDateFormatWeekday", FieldType.STRING, true, 1L, 60L),
044                new FieldDefinition("JavaLongDateFormat", FieldType.STRING, true, 1L, 60L),
045                new FieldDefinition("JavaLongDateFormatWeekday", FieldType.STRING, true, 1L, 60L),
046                new FieldDefinition("JavaTimeFormat", FieldType.STRING, true, 1L, 60L),
047                new FieldDefinition("JavaTimeFormatSeconds", FieldType.STRING, true, 1L, 60L),
048                new FieldDefinition("UnixShortDateFormat", FieldType.STRING, true, 1L, 60L),
049                new FieldDefinition("UnixAbbrevDateFormat", FieldType.STRING, true, 1L, 60L),
050                new FieldDefinition("UnixAbbrevDateFormatWeekday", FieldType.STRING, true, 1L, 60L),
051                new FieldDefinition("UnixLongDateFormat", FieldType.STRING, true, 1L, 60L),
052                new FieldDefinition("UnixLongDateFormatWeekday", FieldType.STRING, true, 1L, 60L),
053                new FieldDefinition("UnixTimeFormat", FieldType.STRING, true, 1L, 60L),
054                new FieldDefinition("UnixTimeFormatSeconds", FieldType.STRING, true, 1L, 60L),
055                new FieldDefinition("ShortDateSeparator", FieldType.STRING, true, 1L, 1L),
056                new FieldDefinition("TimeSeparator", FieldType.STRING, true, 1L, 1L),
057                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
058                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
059                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
060        );
061    }
062
063    @Inject
064    PartyControl partyControl;
065
066    
067    /** Creates a new instance of CreateDateTimeFormatCommand */
068    public CreateDateTimeFormatCommand() {
069        super(null, FORM_FIELD_DEFINITIONS, false);
070    }
071    
072    @Override
073    protected BaseResult execute() {
074        var result = PartyResultFactory.getCreateDateTimeFormatResult();
075        var dateTimeFormatName = form.getDateTimeFormatName();
076        var dateTimeFormat = partyControl.getDateTimeFormatByName(dateTimeFormatName);
077        
078        if(dateTimeFormat == null ) {
079            var javaShortDateFormat = form.getJavaShortDateFormat();
080            var javaAbbrevDateFormat = form.getJavaAbbrevDateFormat();
081            var javaAbbrevDateFormatWeekday = form.getJavaAbbrevDateFormatWeekday();
082            var javaLongDateFormat = form.getJavaLongDateFormat();
083            var javaLongDateFormatWeekday = form.getJavaLongDateFormatWeekday();
084            var javaTimeFormat = form.getJavaTimeFormat();
085            var javaTimeFormatSeconds = form.getJavaTimeFormatSeconds();
086            var unixShortDateFormat = form.getUnixShortDateFormat();
087            var unixAbbrevDateFormat = form.getUnixAbbrevDateFormat();
088            var unixAbbrevDateFormatWeekday = form.getUnixAbbrevDateFormatWeekday();
089            var unixLongDateFormat = form.getUnixLongDateFormat();
090            var unixLongDateFormatWeekday = form.getUnixLongDateFormatWeekday();
091            var unixTimeFormat = form.getUnixTimeFormat();
092            var unixTimeFormatSeconds = form.getUnixTimeFormatSeconds();
093            var shortDateSeparator = form.getShortDateSeparator();
094            var timeSeparator = form.getTimeSeparator();
095            var isDefault = Boolean.valueOf(form.getIsDefault());
096            var sortOrder = Integer.valueOf(form.getSortOrder());
097            var description = form.getDescription();
098            var partyPK = getPartyPK();
099            
100            dateTimeFormat = partyControl.createDateTimeFormat(dateTimeFormatName, javaShortDateFormat, javaAbbrevDateFormat,
101                    javaAbbrevDateFormatWeekday, javaLongDateFormat, javaLongDateFormatWeekday, javaTimeFormat,
102                    javaTimeFormatSeconds, unixShortDateFormat, unixAbbrevDateFormat, unixAbbrevDateFormatWeekday,
103                    unixLongDateFormat, unixLongDateFormatWeekday, unixTimeFormat, unixTimeFormatSeconds, shortDateSeparator,
104                    timeSeparator, isDefault, sortOrder, partyPK);
105            
106            if(description != null) {
107                partyControl.createDateTimeFormatDescription(dateTimeFormat, getPreferredLanguage(), description, partyPK);
108            }
109        } else {
110            addExecutionError(ExecutionErrors.DuplicateDateTimeFormatName.name(), dateTimeFormatName);
111        }
112        
113        if(dateTimeFormat != null) {
114            result.setEntityRef(dateTimeFormat.getPrimaryKey().getEntityRef());
115            result.setDateTimeFormatName(dateTimeFormat.getLastDetail().getDateTimeFormatName());
116        }
117
118        return result;
119    }
120    
121}