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.shipment.server.command;
018
019import com.echothree.control.user.shipment.common.edit.FreeOnBoardDescriptionEdit;
020import com.echothree.control.user.shipment.common.edit.ShipmentEditFactory;
021import com.echothree.control.user.shipment.common.result.EditFreeOnBoardDescriptionResult;
022import com.echothree.control.user.shipment.common.result.ShipmentResultFactory;
023import com.echothree.control.user.shipment.common.spec.FreeOnBoardDescriptionSpec;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.party.server.logic.LanguageLogic;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.control.shipment.server.control.FreeOnBoardControl;
029import com.echothree.model.control.shipment.server.logic.FreeOnBoardLogic;
030import com.echothree.model.data.shipment.server.entity.FreeOnBoard;
031import com.echothree.model.data.shipment.server.entity.FreeOnBoardDescription;
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.BaseEditCommand;
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 EditFreeOnBoardDescriptionCommand
045        extends BaseEditCommand<FreeOnBoardDescriptionSpec, FreeOnBoardDescriptionEdit, EditFreeOnBoardDescriptionResult, FreeOnBoardDescription, FreeOnBoard> {
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.FreeOnBoard.name(), SecurityRoles.Description.name())
056                ))
057        ));
058
059        SPEC_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("FreeOnBoardName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
062        );
063
064        EDIT_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
066        );
067    }
068
069    @Inject
070    FreeOnBoardControl freeOnBoardControl;
071
072    @Inject
073    FreeOnBoardLogic freeOnBoardLogic;
074
075    @Inject
076    LanguageLogic languageLogic;
077
078    /** Creates a new instance of EditFreeOnBoardDescriptionCommand */
079    public EditFreeOnBoardDescriptionCommand() {
080        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
081    }
082
083    @Override
084    public EditFreeOnBoardDescriptionResult getResult() {
085        return ShipmentResultFactory.getEditFreeOnBoardDescriptionResult();
086    }
087
088    @Override
089    public FreeOnBoardDescriptionEdit getEdit() {
090        return ShipmentEditFactory.getFreeOnBoardDescriptionEdit();
091    }
092
093    @Override
094    public FreeOnBoardDescription getEntity(EditFreeOnBoardDescriptionResult result) {
095        FreeOnBoardDescription freeOnBoardDescription = null;
096        var freeOnBoardName = spec.getFreeOnBoardName();
097        var languageIsoName = spec.getLanguageIsoName();
098        var freeOnBoard = freeOnBoardLogic.getFreeOnBoardByName(this, freeOnBoardName);
099        var language = languageLogic.getLanguageByName(this, languageIsoName);
100
101        if(!hasExecutionErrors()) {
102            freeOnBoardDescription = freeOnBoardControl.getFreeOnBoardDescription(freeOnBoard, language,
103                    editModeToEntityPermission(editMode));
104
105            if(freeOnBoardDescription == null) {
106                addExecutionError(ExecutionErrors.UnknownFreeOnBoardDescription.name(), freeOnBoardName, languageIsoName);
107            }
108        }
109
110        return freeOnBoardDescription;
111    }
112
113    @Override
114    public FreeOnBoard getLockEntity(FreeOnBoardDescription freeOnBoardDescription) {
115        return freeOnBoardDescription.getFreeOnBoard();
116    }
117
118    @Override
119    public void fillInResult(EditFreeOnBoardDescriptionResult result, FreeOnBoardDescription freeOnBoardDescription) {
120        result.setFreeOnBoardDescription(freeOnBoardControl.getFreeOnBoardDescriptionTransfer(getUserVisit(), freeOnBoardDescription));
121    }
122
123    @Override
124    public void doLock(FreeOnBoardDescriptionEdit edit, FreeOnBoardDescription freeOnBoardDescription) {
125        edit.setDescription(freeOnBoardDescription.getDescription());
126    }
127
128    @Override
129    public void doUpdate(FreeOnBoardDescription freeOnBoardDescription) {
130        var freeOnBoardDescriptionValue = freeOnBoardControl.getFreeOnBoardDescriptionValue(freeOnBoardDescription);
131
132        freeOnBoardDescriptionValue.setDescription(edit.getDescription());
133
134        freeOnBoardControl.updateFreeOnBoardDescriptionFromValue(freeOnBoardDescriptionValue, getPartyPK());
135    }
136
137}