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.contact.server.command;
018
019import com.echothree.control.user.contact.common.edit.ContactEditFactory;
020import com.echothree.control.user.contact.common.edit.PostalAddressFormatDescriptionEdit;
021import com.echothree.control.user.contact.common.form.EditPostalAddressFormatDescriptionForm;
022import com.echothree.control.user.contact.common.result.ContactResultFactory;
023import com.echothree.control.user.contact.common.spec.PostalAddressFormatDescriptionSpec;
024import com.echothree.model.control.contact.server.control.ContactControl;
025import com.echothree.model.control.party.server.control.PartyControl;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.command.EditMode;
032import com.echothree.util.server.control.BaseEditCommand;
033import java.util.ArrayList;
034import java.util.List;
035import javax.enterprise.context.Dependent;
036import javax.inject.Inject;
037
038@Dependent
039public class EditPostalAddressFormatDescriptionCommand
040        extends BaseEditCommand<PostalAddressFormatDescriptionSpec, PostalAddressFormatDescriptionEdit> {
041    
042    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
043    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
044    
045    static {
046        SPEC_FIELD_DEFINITIONS = List.of(
047                new FieldDefinition("PostalAddressFormatName", FieldType.ENTITY_NAME, true, null, null),
048                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
049        );
050        
051        EDIT_FIELD_DEFINITIONS = List.of(
052                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
053        );
054    }
055
056    @Inject
057    ContactControl contactControl;
058
059    @Inject
060    PartyControl partyControl;
061
062    
063    /** Creates a new instance of EditPostalAddressFormatDescriptionCommand */
064    public EditPostalAddressFormatDescriptionCommand() {
065        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
066    }
067    
068    @Override
069    protected BaseResult execute() {
070        var result = ContactResultFactory.getEditPostalAddressFormatDescriptionResult();
071        var postalAddressFormatName = spec.getPostalAddressFormatName();
072        var postalAddressFormat = contactControl.getPostalAddressFormatByName(postalAddressFormatName);
073        
074        if(postalAddressFormat != null) {
075            var languageIsoName = spec.getLanguageIsoName();
076            var language = partyControl.getLanguageByIsoName(languageIsoName);
077            
078            result.setPostalAddressFormat(contactControl.getPostalAddressFormatTransfer(getUserVisit(), postalAddressFormat));
079            
080            if(language != null) {
081                result.setLanguage(partyControl.getLanguageTransfer(getUserVisit(), language));
082                
083                if(editMode.equals(EditMode.LOCK)) {
084                    var postalAddressFormatDescription = contactControl.getPostalAddressFormatDescription(postalAddressFormat, language);
085                    
086                    if(postalAddressFormatDescription != null) {
087                        result.setPostalAddressFormatDescription(contactControl.getPostalAddressFormatDescriptionTransfer(getUserVisit(), postalAddressFormatDescription));
088                        
089                        if(lockEntity(postalAddressFormat)) {
090                            var edit = ContactEditFactory.getPostalAddressFormatDescriptionEdit();
091                            
092                            result.setEdit(edit);
093                            edit.setDescription(postalAddressFormatDescription.getDescription());
094                        } else {
095                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
096                        }
097                        
098                        result.setEntityLock(getEntityLockTransfer(postalAddressFormat));
099                    } else {
100                        addExecutionError(ExecutionErrors.UnknownPostalAddressFormatDescription.name());
101                    }
102                } else if(editMode.equals(EditMode.UPDATE)) {
103                    var postalAddressFormatDescriptionValue = contactControl.getPostalAddressFormatDescriptionValueForUpdate(postalAddressFormat, language);
104                    
105                    if(postalAddressFormatDescriptionValue != null) {
106                        if(lockEntityForUpdate(postalAddressFormat)) {
107                            try {
108                                var description = edit.getDescription();
109                                
110                                postalAddressFormatDescriptionValue.setDescription(description);
111                                
112                                contactControl.updatePostalAddressFormatDescriptionFromValue(postalAddressFormatDescriptionValue, getPartyPK());
113                            } finally {
114                                unlockEntity(postalAddressFormat);
115                            }
116                        } else {
117                            addExecutionError(ExecutionErrors.EntityLockStale.name());
118                        }
119                    } else {
120                        addExecutionError(ExecutionErrors.UnknownPostalAddressFormatDescription.name());
121                    }
122                }
123            } else {
124                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
125            }
126        } else {
127            addExecutionError(ExecutionErrors.UnknownPostalAddressFormatName.name(), postalAddressFormatName);
128        }
129        
130        return result;
131    }
132    
133}