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