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.result.ContactResultFactory;
022import com.echothree.control.user.contact.common.result.EditPostalAddressFormatDescriptionResult;
023import com.echothree.control.user.contact.common.spec.PostalAddressFormatDescriptionSpec;
024import com.echothree.model.control.contact.server.control.ContactControl;
025import com.echothree.model.control.contact.server.logic.PostalAddressFormatLogic;
026import com.echothree.model.control.party.server.logic.LanguageLogic;
027import com.echothree.model.data.contact.server.entity.PostalAddressFormat;
028import com.echothree.model.data.contact.server.entity.PostalAddressFormatDescription;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.server.control.BaseEditCommand;
033import java.util.List;
034import javax.enterprise.context.Dependent;
035import javax.inject.Inject;
036
037@Dependent
038public class EditPostalAddressFormatDescriptionCommand
039        extends BaseEditCommand<PostalAddressFormatDescriptionSpec, PostalAddressFormatDescriptionEdit, EditPostalAddressFormatDescriptionResult, PostalAddressFormatDescription, PostalAddressFormat> {
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("PostalAddressFormatName", 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    ContactControl contactControl;
057
058    @Inject
059    LanguageLogic languageLogic;
060
061    @Inject
062    PostalAddressFormatLogic postalAddressFormatLogic;
063
064    /** Creates a new instance of EditPostalAddressFormatDescriptionCommand */
065    public EditPostalAddressFormatDescriptionCommand() {
066        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
067    }
068
069    @Override
070    public EditPostalAddressFormatDescriptionResult getResult() {
071        return ContactResultFactory.getEditPostalAddressFormatDescriptionResult();
072    }
073
074    @Override
075    public PostalAddressFormatDescriptionEdit getEdit() {
076        return ContactEditFactory.getPostalAddressFormatDescriptionEdit();
077    }
078
079    @Override
080    public PostalAddressFormatDescription getEntity(EditPostalAddressFormatDescriptionResult result) {
081        PostalAddressFormatDescription postalAddressFormatDescription = null;
082        var postalAddressFormatName = spec.getPostalAddressFormatName();
083        var languageIsoName = spec.getLanguageIsoName();
084        var postalAddressFormat = postalAddressFormatLogic.getPostalAddressFormatByName(this, postalAddressFormatName);
085        var language = languageLogic.getLanguageByName(this, languageIsoName);
086
087        if(!hasExecutionErrors()) {
088            postalAddressFormatDescription = contactControl.getPostalAddressFormatDescription(postalAddressFormat, language,
089                    editModeToEntityPermission(editMode));
090
091            if(postalAddressFormatDescription == null) {
092                addExecutionError(ExecutionErrors.UnknownPostalAddressFormatDescription.name(), postalAddressFormatName, languageIsoName);
093            }
094        }
095
096        return postalAddressFormatDescription;
097    }
098
099    @Override
100    public PostalAddressFormat getLockEntity(PostalAddressFormatDescription postalAddressFormatDescription) {
101        return postalAddressFormatDescription.getPostalAddressFormat();
102    }
103
104    @Override
105    public void fillInResult(EditPostalAddressFormatDescriptionResult result, PostalAddressFormatDescription postalAddressFormatDescription) {
106        result.setPostalAddressFormatDescription(contactControl.getPostalAddressFormatDescriptionTransfer(getUserVisit(), postalAddressFormatDescription));
107    }
108
109    @Override
110    public void doLock(PostalAddressFormatDescriptionEdit edit, PostalAddressFormatDescription postalAddressFormatDescription) {
111        edit.setDescription(postalAddressFormatDescription.getDescription());
112    }
113
114    @Override
115    public void doUpdate(PostalAddressFormatDescription postalAddressFormatDescription) {
116        var postalAddressFormatDescriptionValue = contactControl.getPostalAddressFormatDescriptionValue(postalAddressFormatDescription);
117
118        postalAddressFormatDescriptionValue.setDescription(edit.getDescription());
119
120        contactControl.updatePostalAddressFormatDescriptionFromValue(postalAddressFormatDescriptionValue, getPartyPK());
121    }
122
123}