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.edit.DateTimeFormatDescriptionEdit;
020import com.echothree.control.user.party.common.edit.PartyEditFactory;
021import com.echothree.control.user.party.common.form.EditDateTimeFormatDescriptionForm;
022import com.echothree.control.user.party.common.result.PartyResultFactory;
023import com.echothree.control.user.party.common.spec.DateTimeFormatDescriptionSpec;
024import com.echothree.model.control.party.server.control.PartyControl;
025import com.echothree.model.data.user.common.pk.UserVisitPK;
026import com.echothree.util.common.message.ExecutionErrors;
027import com.echothree.util.common.validation.FieldDefinition;
028import com.echothree.util.common.validation.FieldType;
029import com.echothree.util.common.command.BaseResult;
030import com.echothree.util.common.command.EditMode;
031import com.echothree.util.server.control.BaseEditCommand;
032import java.util.ArrayList;
033import java.util.List;
034import javax.enterprise.context.Dependent;
035import javax.inject.Inject;
036
037@Dependent
038public class EditDateTimeFormatDescriptionCommand
039        extends BaseEditCommand<DateTimeFormatDescriptionSpec, DateTimeFormatDescriptionEdit> {
040    
041    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
042    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
043    
044    static {
045        SPEC_FIELD_DEFINITIONS = List.of(
046                new FieldDefinition("DateTimeFormatName", FieldType.ENTITY_NAME, true, null, null),
047                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
048        );
049        
050        EDIT_FIELD_DEFINITIONS = List.of(
051                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
052        );
053    }
054
055    @Inject
056    PartyControl partyControl;
057
058    
059    /** Creates a new instance of EditDateTimeFormatDescriptionCommand */
060    public EditDateTimeFormatDescriptionCommand() {
061        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
062    }
063    
064    @Override
065    protected BaseResult execute() {
066        var result = PartyResultFactory.getEditDateTimeFormatDescriptionResult();
067        var dateTimeFormatName = spec.getDateTimeFormatName();
068        var dateTimeFormat = partyControl.getDateTimeFormatByName(dateTimeFormatName);
069        
070        if(dateTimeFormat != null) {
071            var languageIsoName = spec.getLanguageIsoName();
072            var language = partyControl.getLanguageByIsoName(languageIsoName);
073            
074            if(language != null) {
075                if(editMode.equals(EditMode.LOCK)) {
076                    var dateTimeFormatDescription = partyControl.getDateTimeFormatDescription(dateTimeFormat, language);
077                    
078                    if(dateTimeFormatDescription != null) {
079                        result.setDateTimeFormatDescription(partyControl.getDateTimeFormatDescriptionTransfer(getUserVisit(), dateTimeFormatDescription));
080                        
081                        if(lockEntity(dateTimeFormat)) {
082                            var edit = PartyEditFactory.getDateTimeFormatDescriptionEdit();
083                            
084                            result.setEdit(edit);
085                            edit.setDescription(dateTimeFormatDescription.getDescription());
086                        } else {
087                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
088                        }
089                        
090                        result.setEntityLock(getEntityLockTransfer(dateTimeFormat));
091                    } else {
092                        addExecutionError(ExecutionErrors.UnknownDateTimeFormatDescription.name());
093                    }
094                } else if(editMode.equals(EditMode.UPDATE)) {
095                    var dateTimeFormatDescriptionValue = partyControl.getDateTimeFormatDescriptionValueForUpdate(dateTimeFormat, language);
096                    
097                    if(dateTimeFormatDescriptionValue != null) {
098                        if(lockEntityForUpdate(dateTimeFormat)) {
099                            try {
100                                var description = edit.getDescription();
101                                
102                                dateTimeFormatDescriptionValue.setDescription(description);
103                                
104                                partyControl.updateDateTimeFormatDescriptionFromValue(dateTimeFormatDescriptionValue, getPartyPK());
105                            } finally {
106                                unlockEntity(dateTimeFormat);
107                            }
108                        } else {
109                            addExecutionError(ExecutionErrors.EntityLockStale.name());
110                        }
111                    } else {
112                        addExecutionError(ExecutionErrors.UnknownDateTimeFormatDescription.name());
113                    }
114                }
115            } else {
116                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
117            }
118        } else {
119            addExecutionError(ExecutionErrors.UnknownDateTimeFormatName.name(), dateTimeFormatName);
120        }
121        
122        return result;
123    }
124    
125}