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.UseEdit; 021import com.echothree.control.user.offer.common.form.EditUseForm; 022import com.echothree.control.user.offer.common.result.OfferResultFactory; 023import com.echothree.control.user.offer.common.spec.UseSpec; 024import com.echothree.model.control.offer.server.control.UseControl; 025import com.echothree.model.control.offer.server.control.UseTypeControl; 026import com.echothree.model.control.offer.server.logic.UseLogic; 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.user.common.pk.UserVisitPK; 031import com.echothree.util.common.command.BaseResult; 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.BaseEditCommand; 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 EditUseCommand 046 extends BaseEditCommand<UseSpec, UseEdit> { 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.Use.name(), SecurityRoles.Edit.name()) 057 )) 058 )); 059 060 SPEC_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("UseName", FieldType.ENTITY_NAME, true, null, null) 062 ); 063 064 EDIT_FIELD_DEFINITIONS = List.of( 065 new FieldDefinition("UseName", FieldType.ENTITY_NAME, true, null, null), 066 new FieldDefinition("UseTypeName", FieldType.ENTITY_NAME, true, null, null), 067 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 068 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 069 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 070 ); 071 } 072 073 /** Creates a new instance of EditUseCommand */ 074 public EditUseCommand() { 075 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 076 } 077 078 @Override 079 protected BaseResult execute() { 080 var useControl = Session.getModelController(UseControl.class); 081 var result = OfferResultFactory.getEditUseResult(); 082 083 if(editMode.equals(EditMode.LOCK)) { 084 var useName = spec.getUseName(); 085 var use = useControl.getUseByName(useName); 086 087 if(use != null) { 088 result.setUse(useControl.getUseTransfer(getUserVisit(), use)); 089 090 if(lockEntity(use)) { 091 var useDescription = useControl.getUseDescription(use, getPreferredLanguage()); 092 var edit = OfferEditFactory.getUseEdit(); 093 var useDetail = use.getLastDetail(); 094 095 result.setEdit(edit); 096 edit.setUseName(useDetail.getUseName()); 097 edit.setUseTypeName(useDetail.getUseType().getLastDetail().getUseTypeName()); 098 edit.setIsDefault(useDetail.getIsDefault().toString()); 099 edit.setSortOrder(useDetail.getSortOrder().toString()); 100 101 if(useDescription != null) { 102 edit.setDescription(useDescription.getDescription()); 103 } 104 } else { 105 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 106 } 107 108 result.setEntityLock(getEntityLockTransfer(use)); 109 } else { 110 addExecutionError(ExecutionErrors.UnknownUseName.name(), useName); 111 } 112 } else if(editMode.equals(EditMode.UPDATE)) { 113 var useName = spec.getUseName(); 114 var use = useControl.getUseByNameForUpdate(useName); 115 116 if(use != null) { 117 useName = edit.getUseName(); 118 var duplicateUse = useControl.getUseByName(useName); 119 120 if(duplicateUse == null || use.equals(duplicateUse)) { 121 var useTypeControl = Session.getModelController(UseTypeControl.class); 122 var useTypeName = edit.getUseTypeName(); 123 var useType = useTypeControl.getUseTypeByName(useTypeName); 124 125 if(useType != null) { 126 if(lockEntityForUpdate(use)) { 127 try { 128 var partyPK = getPartyPK(); 129 var useDetailValue = useControl.getUseDetailValueForUpdate(use); 130 var useDescription = useControl.getUseDescriptionForUpdate(use, getPreferredLanguage()); 131 var description = edit.getDescription(); 132 133 useDetailValue.setUseName(edit.getUseName()); 134 useDetailValue.setUseTypePK(useType.getPrimaryKey()); 135 useDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 136 useDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 137 138 UseLogic.getInstance().updateUseFromValue(useDetailValue, partyPK); 139 140 if(useDescription == null && description != null) { 141 useControl.createUseDescription(use, getPreferredLanguage(), description, partyPK); 142 } else if(useDescription != null && description == null) { 143 useControl.deleteUseDescription(useDescription, partyPK); 144 } else if(useDescription != null && description != null) { 145 var useDescriptionValue = useControl.getUseDescriptionValue(useDescription); 146 147 useDescriptionValue.setDescription(description); 148 useControl.updateUseDescriptionFromValue(useDescriptionValue, partyPK); 149 } 150 } finally { 151 unlockEntity(use); 152 } 153 } else { 154 addExecutionError(ExecutionErrors.EntityLockStale.name()); 155 } 156 } else { 157 addExecutionError(ExecutionErrors.UnknownUseTypeName.name(), useTypeName); 158 } 159 } else { 160 addExecutionError(ExecutionErrors.DuplicateUseName.name(), useName); 161 } 162 } else { 163 addExecutionError(ExecutionErrors.UnknownUseName.name(), useName); 164 } 165 } 166 167 return result; 168 } 169 170}