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.offer.server.command;
018
019import com.echothree.control.user.offer.common.edit.OfferEditFactory;
020import com.echothree.control.user.offer.common.edit.OfferNameElementDescriptionEdit;
021import com.echothree.control.user.offer.common.result.EditOfferNameElementDescriptionResult;
022import com.echothree.control.user.offer.common.result.OfferResultFactory;
023import com.echothree.control.user.offer.common.spec.OfferNameElementDescriptionSpec;
024import com.echothree.model.control.offer.server.control.OfferNameElementControl;
025import com.echothree.model.control.offer.server.logic.OfferNameElementLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.offer.server.entity.OfferNameElement;
030import com.echothree.model.data.offer.server.entity.OfferNameElementDescription;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.server.control.BaseAbstractEditCommand;
034import com.echothree.util.server.control.CommandSecurityDefinition;
035import com.echothree.util.server.control.PartyTypeDefinition;
036import com.echothree.util.server.control.SecurityRoleDefinition;
037import java.util.List;
038import javax.enterprise.context.Dependent;
039import javax.inject.Inject;
040
041@Dependent
042public class EditOfferNameElementDescriptionCommand
043        extends BaseAbstractEditCommand<OfferNameElementDescriptionSpec, OfferNameElementDescriptionEdit, EditOfferNameElementDescriptionResult, OfferNameElementDescription, OfferNameElement> {
044
045    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
046    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
047    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
048
049    static {
050        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
051                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
052                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
053                        new SecurityRoleDefinition(SecurityRoleGroups.OfferNameElement.name(), SecurityRoles.Description.name())
054                ))
055        ));
056
057        SPEC_FIELD_DEFINITIONS = List.of(
058                new FieldDefinition("OfferNameElementName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
060        );
061
062        EDIT_FIELD_DEFINITIONS = List.of(
063                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
064        );
065    }
066
067    @Inject
068    OfferNameElementControl offerNameElementControl;
069
070    @Inject
071    OfferNameElementLogic offerNameElementLogic;
072
073    /** Creates a new instance of EditOfferNameElementDescriptionCommand */
074    public EditOfferNameElementDescriptionCommand() {
075        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077
078    @Override
079    public EditOfferNameElementDescriptionResult getResult() {
080        return OfferResultFactory.getEditOfferNameElementDescriptionResult();
081    }
082
083    @Override
084    public OfferNameElementDescriptionEdit getEdit() {
085        return OfferEditFactory.getOfferNameElementDescriptionEdit();
086    }
087
088    @Override
089    public OfferNameElementDescription getEntity(EditOfferNameElementDescriptionResult result) {
090        return offerNameElementLogic.getOfferNameElementDescriptionBySpec(this, spec, editModeToEntityPermission(editMode));
091    }
092
093    @Override
094    public OfferNameElement getLockEntity(OfferNameElementDescription offerNameElementDescription) {
095        return offerNameElementDescription.getOfferNameElement();
096    }
097
098    @Override
099    public void fillInResult(EditOfferNameElementDescriptionResult result, OfferNameElementDescription offerNameElementDescription) {
100        result.setOfferNameElementDescription(offerNameElementControl.getOfferNameElementDescriptionTransfer(getUserVisit(), offerNameElementDescription));
101    }
102
103    @Override
104    public void doLock(OfferNameElementDescriptionEdit edit, OfferNameElementDescription offerNameElementDescription) {
105        edit.setDescription(offerNameElementDescription.getDescription());
106    }
107
108    @Override
109    public void doUpdate(OfferNameElementDescription offerNameElementDescription) {
110        var offerNameElementDescriptionValue = offerNameElementControl.getOfferNameElementDescriptionValue(offerNameElementDescription);
111
112        offerNameElementDescriptionValue.setDescription(edit.getDescription());
113
114        offerNameElementControl.updateOfferNameElementDescriptionFromValue(offerNameElementDescriptionValue, getPartyPK());
115    }
116
117}