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