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.SourceEdit;
021import com.echothree.control.user.offer.common.form.EditSourceForm;
022import com.echothree.control.user.offer.common.result.EditSourceResult;
023import com.echothree.control.user.offer.common.result.OfferResultFactory;
024import com.echothree.control.user.offer.common.spec.SourceSpec;
025import com.echothree.model.control.offer.server.control.SourceControl;
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.Source;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.command.EditMode;
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 EditSourceCommand
045        extends BaseAbstractEditCommand<SourceSpec, SourceEdit, EditSourceResult, Source, Source> {
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.Source.name(), SecurityRoles.Edit.name())
056                ))
057        ));
058
059        SPEC_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("SourceName", FieldType.ENTITY_NAME, true, null, null)
061        );
062
063        EDIT_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("SourceName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
066                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
067        );
068    }
069
070    @Inject
071    SourceControl sourceControl;
072
073    /** Creates a new instance of EditSourceCommand */
074    public EditSourceCommand() {
075        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077
078    @Override
079    public EditSourceResult getResult() {
080        return OfferResultFactory.getEditSourceResult();
081    }
082
083    @Override
084    public SourceEdit getEdit() {
085        return OfferEditFactory.getSourceEdit();
086    }
087
088    @Override
089    public Source getEntity(EditSourceResult result) {
090        Source source;
091        var sourceName = spec.getSourceName();
092
093        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
094            source = sourceControl.getSourceByName(sourceName);
095        } else { // EditMode.UPDATE
096            source = sourceControl.getSourceByNameForUpdate(sourceName);
097        }
098
099        if(source != null) {
100            result.setSource(sourceControl.getSourceTransfer(getUserVisit(), source));
101        } else {
102            addExecutionError(ExecutionErrors.UnknownSourceName.name(), sourceName);
103        }
104
105        return source;
106    }
107
108    @Override
109    public Source getLockEntity(Source source) {
110        return source;
111    }
112
113    @Override
114    public void fillInResult(EditSourceResult result, Source source) {
115        result.setSource(sourceControl.getSourceTransfer(getUserVisit(), source));
116    }
117
118    @Override
119    public void doLock(SourceEdit edit, Source source) {
120        var sourceDetail = source.getLastDetail();
121
122        edit.setSourceName(sourceDetail.getSourceName());
123        edit.setIsDefault(sourceDetail.getIsDefault().toString());
124        edit.setSortOrder(sourceDetail.getSortOrder().toString());
125    }
126
127    @Override
128    public void canUpdate(Source source) {
129        var sourceName = edit.getSourceName();
130        var duplicateSource = sourceControl.getSourceByName(sourceName);
131
132        if(duplicateSource != null && !source.equals(duplicateSource)) {
133            addExecutionError(ExecutionErrors.DuplicateSourceName.name(), sourceName);
134        }
135    }
136
137    @Override
138    public void doUpdate(Source source) {
139        var partyPK = getPartyPK();
140        var sourceDetailValue = sourceControl.getSourceDetailValueForUpdate(source);
141
142        sourceDetailValue.setSourceName(edit.getSourceName());
143        sourceDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
144        sourceDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
145
146        sourceControl.updateSourceFromValue(sourceDetailValue, partyPK);
147    }
148
149}