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.UseEdit;
021import com.echothree.control.user.offer.common.result.EditUseResult;
022import com.echothree.control.user.offer.common.result.OfferResultFactory;
023import com.echothree.control.user.offer.common.spec.UseSpec;
024import com.echothree.model.control.offer.server.control.UseControl;
025import com.echothree.model.control.offer.server.logic.UseLogic;
026import com.echothree.model.control.offer.server.logic.UseTypeLogic;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.offer.server.entity.Use;
031import com.echothree.model.data.offer.server.entity.UseType;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseAbstractEditCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class EditUseCommand
045        extends BaseAbstractEditCommand<UseSpec, UseEdit, EditUseResult, Use, Use> {
046
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
049    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
050
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.Use.name(), SecurityRoles.Edit.name())
056                ))
057        ));
058
059        SPEC_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("UseName", FieldType.ENTITY_NAME, true, null, null)
061        );
062
063        EDIT_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("UseName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("UseTypeName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
067                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
068                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
069        );
070    }
071
072    @Inject
073    UseControl useControl;
074
075    @Inject
076    UseLogic useLogic;
077
078    @Inject
079    UseTypeLogic useTypeLogic;
080
081    /** Creates a new instance of EditUseCommand */
082    public EditUseCommand() {
083        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
084    }
085
086    @Override
087    public EditUseResult getResult() {
088        return OfferResultFactory.getEditUseResult();
089    }
090
091    @Override
092    public UseEdit getEdit() {
093        return OfferEditFactory.getUseEdit();
094    }
095
096    @Override
097    public Use getEntity(EditUseResult result) {
098        return useLogic.getUseByName(this, spec.getUseName(), editModeToEntityPermission(editMode));
099    }
100
101    @Override
102    public Use getLockEntity(Use use) {
103        return use;
104    }
105
106    @Override
107    public void fillInResult(EditUseResult result, Use use) {
108        result.setUse(useControl.getUseTransfer(getUserVisit(), use));
109    }
110
111    private UseType useType;
112
113    @Override
114    public void doLock(UseEdit edit, Use use) {
115        var useDescription = useControl.getUseDescription(use, getPreferredLanguage());
116        var useDetail = use.getLastDetail();
117
118        edit.setUseName(useDetail.getUseName());
119        edit.setUseTypeName(useDetail.getUseType().getLastDetail().getUseTypeName());
120        edit.setIsDefault(useDetail.getIsDefault().toString());
121        edit.setSortOrder(useDetail.getSortOrder().toString());
122
123        if(useDescription != null) {
124            edit.setDescription(useDescription.getDescription());
125        }
126    }
127
128    @Override
129    public void canUpdate(Use use) {
130        var useName = edit.getUseName();
131        var duplicateUse = useControl.getUseByName(useName);
132
133        if(duplicateUse == null || use.equals(duplicateUse)) {
134            useType = useTypeLogic.getUseTypeByName(this, edit.getUseTypeName());
135        } else {
136            addExecutionError(ExecutionErrors.DuplicateUseName.name(), useName);
137        }
138    }
139
140    @Override
141    public void doUpdate(Use use) {
142        var partyPK = getPartyPK();
143        var useDetailValue = useControl.getUseDetailValueForUpdate(use);
144        var useDescription = useControl.getUseDescriptionForUpdate(use, getPreferredLanguage());
145        var description = edit.getDescription();
146
147        useDetailValue.setUseName(edit.getUseName());
148        useDetailValue.setUseTypePK(useType.getPrimaryKey());
149        useDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
150        useDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
151
152        useLogic.updateUseFromValue(useDetailValue, partyPK);
153
154        if(useDescription == null && description != null) {
155            useControl.createUseDescription(use, getPreferredLanguage(), description, partyPK);
156        } else if(useDescription != null && description == null) {
157            useControl.deleteUseDescription(useDescription, partyPK);
158        } else if(useDescription != null && description != null) {
159            var useDescriptionValue = useControl.getUseDescriptionValue(useDescription);
160
161            useDescriptionValue.setDescription(description);
162            useControl.updateUseDescriptionFromValue(useDescriptionValue, partyPK);
163        }
164    }
165
166}