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