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