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.subscription.server.command;
018
019import com.echothree.control.user.subscription.common.form.CreateSubscriptionForm;
020import com.echothree.model.control.party.common.PartyTypes;
021import com.echothree.model.control.party.server.control.PartyControl;
022import com.echothree.model.control.security.common.SecurityRoleGroups;
023import com.echothree.model.control.security.common.SecurityRoles;
024import com.echothree.model.control.subscription.server.control.SubscriptionControl;
025import com.echothree.model.control.subscription.server.logic.SubscriptionLogic;
026import com.echothree.model.control.uom.common.UomConstants;
027import com.echothree.model.control.uom.server.control.UomControl;
028import com.echothree.model.control.uom.server.util.Conversion;
029import com.echothree.model.data.uom.server.entity.UnitOfMeasureType;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseSimpleCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import com.echothree.util.server.persistence.Session;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041
042@Dependent
043public class CreateSubscriptionCommand
044        extends BaseSimpleCommand<CreateSubscriptionForm> {
045
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
048    
049    static {
050        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
051                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
052                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
053                        new SecurityRoleDefinition(SecurityRoleGroups.Subscription.name(), SecurityRoles.Create.name())
054                ))
055        ));
056
057        FORM_FIELD_DEFINITIONS = List.of(
058                new FieldDefinition("SubscriptionKindName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("SubscriptionTypeName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("SubscriptionTime", FieldType.UNSIGNED_LONG, false, null, null)
063        );
064    }
065    
066    /** Creates a new instance of CreateSubscriptionCommand */
067    public CreateSubscriptionCommand() {
068        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
069    }
070    
071    @Override
072    protected BaseResult execute() {
073        var subscriptionControl = Session.getModelController(SubscriptionControl.class);
074        var subscriptionKindName = form.getSubscriptionKindName();
075        var subscriptionKind = subscriptionControl.getSubscriptionKindByName(subscriptionKindName);
076        
077        if(subscriptionKind != null) {
078            var subscriptionTypeName = form.getSubscriptionTypeName();
079            var subscriptionType = subscriptionControl.getSubscriptionTypeByName(subscriptionKind, subscriptionTypeName);
080            
081            if(subscriptionType != null) {
082                var partyControl = Session.getModelController(PartyControl.class);
083                var partyName = form.getPartyName();
084                var party = partyControl.getPartyByName(partyName);
085                
086                if(party != null) {
087                    var subscription = subscriptionControl.getSubscription(subscriptionType, party);
088                    
089                    if(subscription == null) {
090                        var uomControl = Session.getModelController(UomControl.class);
091                        var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
092                        UnitOfMeasureType unitOfMeasureType = null;
093                        
094                        if(unitOfMeasureTypeName != null) {
095                            var unitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_TIME);
096                            
097                            unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
098                        }
099                        
100                        if(unitOfMeasureTypeName == null || unitOfMeasureType != null) {
101                            var strSubscriptionTime = form.getSubscriptionTime();
102                            var subscriptionTime = strSubscriptionTime == null? null: Long.valueOf(strSubscriptionTime);
103                            
104                            if(unitOfMeasureTypeName == null || subscriptionTime != null) {
105                                Long endTime = null;
106                                var createdBy = getPartyPK();
107                                
108                                if(subscriptionTime != null) {
109                                    var conversion = new Conversion(uomControl, unitOfMeasureType, subscriptionTime).convertToLowestUnitOfMeasureType();
110                                    endTime = session.getStartTime() + conversion.getQuantity();
111                                }
112                                
113                                // ExecutionErrorAccumulator is passed in as null so that an Exception will be thrown if there is an error.
114                                SubscriptionLogic.getInstance().createSubscription(null, session, subscriptionType, party, endTime, createdBy);
115                            } else {
116                                addExecutionError(ExecutionErrors.MissingSubscriptionTime.name());
117                            }
118                        } else {
119                            addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
120                        }
121                    } else {
122                        addExecutionError(ExecutionErrors.DuplicateSubscription.name());
123                    }
124                } else {
125                    addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
126                }
127            } else {
128                addExecutionError(ExecutionErrors.UnknownSubscriptionTypeName.name(), subscriptionTypeName);
129            }
130        } else {
131            addExecutionError(ExecutionErrors.UnknownSubscriptionKindName.name(), subscriptionKindName);
132        }
133        
134        return null;
135    }
136    
137}