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