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