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.UseNameElementEdit;
021import com.echothree.control.user.offer.common.form.EditUseNameElementForm;
022import com.echothree.control.user.offer.common.result.EditUseNameElementResult;
023import com.echothree.control.user.offer.common.result.OfferResultFactory;
024import com.echothree.control.user.offer.common.spec.UseNameElementUniversalSpec;
025import com.echothree.model.control.offer.server.control.UseNameElementControl;
026import com.echothree.model.control.offer.server.logic.UseNameElementLogic;
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.UseNameElement;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
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 EditUseNameElementCommand
045        extends BaseAbstractEditCommand<UseNameElementUniversalSpec, UseNameElementEdit, EditUseNameElementResult, UseNameElement, UseNameElement> {
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.UseNameElement.name(), SecurityRoles.Edit.name())
056                ))
057        ));
058        
059        SPEC_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("UseNameElementName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
062                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
063        );
064        
065        EDIT_FIELD_DEFINITIONS = List.of(
066                new FieldDefinition("UseNameElementName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("Offset", FieldType.UNSIGNED_INTEGER, true, null, null),
068                new FieldDefinition("Length", FieldType.UNSIGNED_INTEGER, true, null, null),
069                new FieldDefinition("ValidationPattern", FieldType.REGULAR_EXPRESSION, false, null, null),
070                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
071        );
072    }
073
074    @Inject
075    UseNameElementControl useNameElementControl;
076
077    @Inject
078    UseNameElementLogic useNameElementLogic;
079
080    
081    /** Creates a new instance of EditUseNameElementCommand */
082    public EditUseNameElementCommand() {
083        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
084    }
085    
086    @Override
087    public EditUseNameElementResult getResult() {
088        return OfferResultFactory.getEditUseNameElementResult();
089    }
090    
091    @Override
092    public UseNameElementEdit getEdit() {
093        return OfferEditFactory.getUseNameElementEdit();
094    }
095    
096    @Override
097    public UseNameElement getEntity(EditUseNameElementResult result) {
098        return useNameElementLogic.getUseNameElementByUniversalSpec(this, spec, editModeToEntityPermission(editMode));
099    }
100    
101    @Override
102    public UseNameElement getLockEntity(UseNameElement useNameElement) {
103        return useNameElement;
104    }
105    
106    @Override
107    public void fillInResult(EditUseNameElementResult result, UseNameElement useNameElement) {
108        result.setUseNameElement(useNameElementControl.getUseNameElementTransfer(getUserVisit(), useNameElement));
109    }
110    
111    @Override
112    public void doLock(UseNameElementEdit edit, UseNameElement useNameElement) {
113        var useNameElementDescription = useNameElementControl.getUseNameElementDescription(useNameElement, getPreferredLanguage());
114        var useNameElementDetail = useNameElement.getLastDetail();
115        
116        edit.setUseNameElementName(useNameElementDetail.getUseNameElementName());
117        edit.setOffset(useNameElementDetail.getOffset().toString());
118        edit.setLength(useNameElementDetail.getLength().toString());
119        edit.setValidationPattern(useNameElementDetail.getValidationPattern());
120
121        if(useNameElementDescription != null) {
122            edit.setDescription(useNameElementDescription.getDescription());
123        }
124    }
125        
126    @Override
127    public void canUpdate(UseNameElement useNameElement) {
128        var useNameElementName = edit.getUseNameElementName();
129        var duplicateUseNameElement = useNameElementControl.getUseNameElementByName(useNameElementName);
130
131        if(duplicateUseNameElement != null && !useNameElement.equals(duplicateUseNameElement)) {
132            addExecutionError(ExecutionErrors.DuplicateUseNameElementName.name(), useNameElementName);
133        }
134    }
135    
136    @Override
137    public void doUpdate(UseNameElement useNameElement) {
138        var partyPK = getPartyPK();
139        var useNameElementDetailValue = useNameElementControl.getUseNameElementDetailValueForUpdate(useNameElement);
140        var useNameElementDescription = useNameElementControl.getUseNameElementDescriptionForUpdate(useNameElement, getPreferredLanguage());
141        var description = edit.getDescription();
142
143        useNameElementDetailValue.setUseNameElementName(edit.getUseNameElementName());
144        useNameElementDetailValue.setOffset(Integer.valueOf(edit.getOffset()));
145        useNameElementDetailValue.setLength(Integer.valueOf(edit.getLength()));
146        useNameElementDetailValue.setValidationPattern(edit.getValidationPattern());
147
148        useNameElementControl.updateUseNameElementFromValue(useNameElementDetailValue, partyPK);
149
150        if(useNameElementDescription == null && description != null) {
151            useNameElementControl.createUseNameElementDescription(useNameElement, getPreferredLanguage(), description, partyPK);
152        } else if(useNameElementDescription != null && description == null) {
153            useNameElementControl.deleteUseNameElementDescription(useNameElementDescription, partyPK);
154        } else if(useNameElementDescription != null && description != null) {
155            var useNameElementDescriptionValue = useNameElementControl.getUseNameElementDescriptionValue(useNameElementDescription);
156
157            useNameElementDescriptionValue.setDescription(description);
158            useNameElementControl.updateUseNameElementDescriptionFromValue(useNameElementDescriptionValue, partyPK);
159        }
160    }
161    
162}