001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.party.server.entity.DateTimeFormat;
022import com.echothree.model.data.user.common.pk.UserVisitPK;
023import com.echothree.util.common.message.ExecutionErrors;
024import com.echothree.util.common.validation.FieldDefinition;
025import com.echothree.util.common.validation.FieldType;
026import com.echothree.util.common.command.BaseResult;
027import com.echothree.util.server.control.BaseSimpleCommand;
028import com.echothree.util.server.persistence.Session;
029import java.util.Arrays;
030import java.util.Collections;
031import java.util.List;
032
033public class CreateDateTimeFormatCommand
034        extends BaseSimpleCommand<CreateDateTimeFormatForm> {
035
036    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
037
038    static {
039        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
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    /** Creates a new instance of CreateDateTimeFormatCommand */
064    public CreateDateTimeFormatCommand(UserVisitPK userVisitPK, CreateDateTimeFormatForm form) {
065        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
066    }
067    
068    @Override
069    protected BaseResult execute() {
070        var partyControl = Session.getModelController(PartyControl.class);
071        String dateTimeFormatName = form.getDateTimeFormatName();
072        DateTimeFormat dateTimeFormat = partyControl.getDateTimeFormatByName(dateTimeFormatName);
073        
074        if(dateTimeFormat == null ) {
075            String javaShortDateFormat = form.getJavaShortDateFormat();
076            String javaAbbrevDateFormat = form.getJavaAbbrevDateFormat();
077            String javaAbbrevDateFormatWeekday = form.getJavaAbbrevDateFormatWeekday();
078            String javaLongDateFormat = form.getJavaLongDateFormat();
079            String javaLongDateFormatWeekday = form.getJavaLongDateFormatWeekday();
080            String javaTimeFormat = form.getJavaTimeFormat();
081            String javaTimeFormatSeconds = form.getJavaTimeFormatSeconds();
082            String unixShortDateFormat = form.getUnixShortDateFormat();
083            String unixAbbrevDateFormat = form.getUnixAbbrevDateFormat();
084            String unixAbbrevDateFormatWeekday = form.getUnixAbbrevDateFormatWeekday();
085            String unixLongDateFormat = form.getUnixLongDateFormat();
086            String unixLongDateFormatWeekday = form.getUnixLongDateFormatWeekday();
087            String unixTimeFormat = form.getUnixTimeFormat();
088            String unixTimeFormatSeconds = form.getUnixTimeFormatSeconds();
089            String shortDateSeparator = form.getShortDateSeparator();
090            String timeSeparator = form.getTimeSeparator();
091            var isDefault = Boolean.valueOf(form.getIsDefault());
092            var sortOrder = Integer.valueOf(form.getSortOrder());
093            var description = form.getDescription();
094            var partyPK = getPartyPK();
095            
096            dateTimeFormat = partyControl.createDateTimeFormat(dateTimeFormatName, javaShortDateFormat, javaAbbrevDateFormat,
097                    javaAbbrevDateFormatWeekday, javaLongDateFormat, javaLongDateFormatWeekday, javaTimeFormat,
098                    javaTimeFormatSeconds, unixShortDateFormat, unixAbbrevDateFormat, unixAbbrevDateFormatWeekday,
099                    unixLongDateFormat, unixLongDateFormatWeekday, unixTimeFormat, unixTimeFormatSeconds, shortDateSeparator,
100                    timeSeparator, isDefault, sortOrder, partyPK);
101            
102            if(description != null) {
103                partyControl.createDateTimeFormatDescription(dateTimeFormat, getPreferredLanguage(), description, partyPK);
104            }
105        } else {
106            addExecutionError(ExecutionErrors.DuplicateDateTimeFormatName.name(), dateTimeFormatName);
107        }
108        
109        return null;
110    }
111    
112}