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 com.echothree.util.server.persistence.Session;
028import java.util.List;
029import javax.enterprise.context.Dependent;
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    /** Creates a new instance of CreateDateTimeFormatCommand */
063    public CreateDateTimeFormatCommand() {
064        super(null, FORM_FIELD_DEFINITIONS, false);
065    }
066    
067    @Override
068    protected BaseResult execute() {
069        var partyControl = Session.getModelController(PartyControl.class);
070        var dateTimeFormatName = form.getDateTimeFormatName();
071        var dateTimeFormat = partyControl.getDateTimeFormatByName(dateTimeFormatName);
072        
073        if(dateTimeFormat == null ) {
074            var javaShortDateFormat = form.getJavaShortDateFormat();
075            var javaAbbrevDateFormat = form.getJavaAbbrevDateFormat();
076            var javaAbbrevDateFormatWeekday = form.getJavaAbbrevDateFormatWeekday();
077            var javaLongDateFormat = form.getJavaLongDateFormat();
078            var javaLongDateFormatWeekday = form.getJavaLongDateFormatWeekday();
079            var javaTimeFormat = form.getJavaTimeFormat();
080            var javaTimeFormatSeconds = form.getJavaTimeFormatSeconds();
081            var unixShortDateFormat = form.getUnixShortDateFormat();
082            var unixAbbrevDateFormat = form.getUnixAbbrevDateFormat();
083            var unixAbbrevDateFormatWeekday = form.getUnixAbbrevDateFormatWeekday();
084            var unixLongDateFormat = form.getUnixLongDateFormat();
085            var unixLongDateFormatWeekday = form.getUnixLongDateFormatWeekday();
086            var unixTimeFormat = form.getUnixTimeFormat();
087            var unixTimeFormatSeconds = form.getUnixTimeFormatSeconds();
088            var shortDateSeparator = form.getShortDateSeparator();
089            var timeSeparator = form.getTimeSeparator();
090            var isDefault = Boolean.valueOf(form.getIsDefault());
091            var sortOrder = Integer.valueOf(form.getSortOrder());
092            var description = form.getDescription();
093            var partyPK = getPartyPK();
094            
095            dateTimeFormat = partyControl.createDateTimeFormat(dateTimeFormatName, javaShortDateFormat, javaAbbrevDateFormat,
096                    javaAbbrevDateFormatWeekday, javaLongDateFormat, javaLongDateFormatWeekday, javaTimeFormat,
097                    javaTimeFormatSeconds, unixShortDateFormat, unixAbbrevDateFormat, unixAbbrevDateFormatWeekday,
098                    unixLongDateFormat, unixLongDateFormatWeekday, unixTimeFormat, unixTimeFormatSeconds, shortDateSeparator,
099                    timeSeparator, isDefault, sortOrder, partyPK);
100            
101            if(description != null) {
102                partyControl.createDateTimeFormatDescription(dateTimeFormat, getPreferredLanguage(), description, partyPK);
103            }
104        } else {
105            addExecutionError(ExecutionErrors.DuplicateDateTimeFormatName.name(), dateTimeFormatName);
106        }
107        
108        return null;
109    }
110    
111}