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.core.server.command;
018
019import com.echothree.control.user.core.common.edit.CoreEditFactory;
020import com.echothree.control.user.core.common.edit.ServiceEdit;
021import com.echothree.control.user.core.common.form.EditServiceForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditServiceResult;
024import com.echothree.control.user.core.common.spec.ServiceSpec;
025import com.echothree.model.control.core.server.control.ServerControl;
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.core.server.entity.Protocol;
030import com.echothree.model.data.core.server.entity.Service;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.command.EditMode;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.server.control.BaseAbstractEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.List;
042import javax.enterprise.context.Dependent;
043
044@Dependent
045public class EditServiceCommand
046        extends BaseAbstractEditCommand<ServiceSpec, ServiceEdit, EditServiceResult, Service, Service> {
047    
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
050    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.Service.name(), SecurityRoles.Edit.name())
057                        ))
058                ));
059        
060        SPEC_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("ServiceName", FieldType.ENTITY_NAME, true, null, null)
062                );
063        
064        EDIT_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("ServiceName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("Port", FieldType.UNSIGNED_INTEGER, true, 1L, 65535L),
067                new FieldDefinition("ProtocolName", FieldType.ENTITY_NAME, true, null, null),
068                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
069                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
070                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
071                );
072    }
073    
074    /** Creates a new instance of EditServiceCommand */
075    public EditServiceCommand() {
076        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
077    }
078    
079    @Override
080    public EditServiceResult getResult() {
081        return CoreResultFactory.getEditServiceResult();
082    }
083
084    @Override
085    public ServiceEdit getEdit() {
086        return CoreEditFactory.getServiceEdit();
087    }
088
089    @Override
090    public Service getEntity(EditServiceResult result) {
091        var serverControl = Session.getModelController(ServerControl.class);
092        Service service;
093        var serviceName = spec.getServiceName();
094
095        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
096            service = serverControl.getServiceByName(serviceName);
097        } else { // EditMode.UPDATE
098            service = serverControl.getServiceByNameForUpdate(serviceName);
099        }
100
101        if(service != null) {
102            result.setService(serverControl.getServiceTransfer(getUserVisit(), service));
103        } else {
104            addExecutionError(ExecutionErrors.UnknownServiceName.name(), serviceName);
105        }
106
107        return service;
108    }
109
110    @Override
111    public Service getLockEntity(Service service) {
112        return service;
113    }
114
115    @Override
116    public void fillInResult(EditServiceResult result, Service service) {
117        var serverControl = Session.getModelController(ServerControl.class);
118
119        result.setService(serverControl.getServiceTransfer(getUserVisit(), service));
120    }
121
122    @Override
123    public void doLock(ServiceEdit edit, Service service) {
124        var serverControl = Session.getModelController(ServerControl.class);
125        var serviceDescription = serverControl.getServiceDescription(service, getPreferredLanguage());
126        var serviceDetail = service.getLastDetail();
127
128        edit.setServiceName(serviceDetail.getServiceName());
129        edit.setPort(serviceDetail.getPort().toString());
130        edit.setProtocolName(serviceDetail.getProtocol().getLastDetail().getProtocolName());
131        edit.setIsDefault(serviceDetail.getIsDefault().toString());
132        edit.setSortOrder(serviceDetail.getSortOrder().toString());
133
134        if(serviceDescription != null) {
135            edit.setDescription(serviceDescription.getDescription());
136        }
137    }
138
139    Protocol protocol;
140
141    @Override
142    public void canUpdate(Service service) {
143        var serverControl = Session.getModelController(ServerControl.class);
144        var serviceName = edit.getServiceName();
145        var duplicateService = serverControl.getServiceByName(serviceName);
146
147        if(duplicateService != null && !service.equals(duplicateService)) {
148            addExecutionError(ExecutionErrors.DuplicateServiceName.name(), serviceName);
149        } else {
150            var protocolName = edit.getProtocolName();
151
152            protocol = serverControl.getProtocolByName(protocolName);
153
154            if(protocol == null) {
155                addExecutionError(ExecutionErrors.UnknownProtocolName.name(), protocolName);
156            }
157        }
158    }
159
160    @Override
161    public void doUpdate(Service service) {
162        var serverControl = Session.getModelController(ServerControl.class);
163        var partyPK = getPartyPK();
164        var serviceDetailValue = serverControl.getServiceDetailValueForUpdate(service);
165        var serviceDescription = serverControl.getServiceDescriptionForUpdate(service, getPreferredLanguage());
166        var description = edit.getDescription();
167
168        serviceDetailValue.setServiceName(edit.getServiceName());
169        serviceDetailValue.setPort(Integer.valueOf(edit.getPort()));
170        serviceDetailValue.setProtocolPK(protocol.getPrimaryKey());
171        serviceDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
172        serviceDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
173
174        serverControl.updateServiceFromValue(serviceDetailValue, partyPK);
175
176        if(serviceDescription == null && description != null) {
177            serverControl.createServiceDescription(service, getPreferredLanguage(), description, partyPK);
178        } else {
179            if(serviceDescription != null && description == null) {
180                serverControl.deleteServiceDescription(serviceDescription, partyPK);
181            } else {
182                if(serviceDescription != null && description != null) {
183                    var serviceDescriptionValue = serverControl.getServiceDescriptionValue(serviceDescription);
184
185                    serviceDescriptionValue.setDescription(description);
186                    serverControl.updateServiceDescriptionFromValue(serviceDescriptionValue, partyPK);
187                }
188            }
189        }
190    }
191    
192}