001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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.cancellationpolicy.server.command; 018 019import com.echothree.control.user.cancellationpolicy.common.edit.CancellationKindDescriptionEdit; 020import com.echothree.control.user.cancellationpolicy.common.edit.CancellationPolicyEditFactory; 021import com.echothree.control.user.cancellationpolicy.common.form.EditCancellationKindDescriptionForm; 022import com.echothree.control.user.cancellationpolicy.common.result.CancellationPolicyResultFactory; 023import com.echothree.control.user.cancellationpolicy.common.spec.CancellationKindDescriptionSpec; 024import com.echothree.model.control.cancellationpolicy.server.control.CancellationPolicyControl; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.party.server.control.PartyControl; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 030import com.echothree.util.common.message.ExecutionErrors; 031import com.echothree.util.common.validation.FieldDefinition; 032import com.echothree.util.common.validation.FieldType; 033import com.echothree.util.common.command.BaseResult; 034import com.echothree.util.common.command.EditMode; 035import com.echothree.util.server.control.BaseEditCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import com.echothree.util.server.persistence.Session; 040import java.util.ArrayList; 041import java.util.Arrays; 042import java.util.Collections; 043import java.util.List; 044import javax.enterprise.context.RequestScoped; 045 046@RequestScoped 047public class EditCancellationKindDescriptionCommand 048 extends BaseEditCommand<CancellationKindDescriptionSpec, CancellationKindDescriptionEdit> { 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.CancellationKind.name(), SecurityRoles.Description.name()) 059 ))) 060 ))); 061 062 List<FieldDefinition> temp = new ArrayList<>(2); 063 temp.add(new FieldDefinition("CancellationKindName", FieldType.ENTITY_NAME, true, null, null)); 064 temp.add(new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)); 065 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 066 067 temp = new ArrayList<>(1); 068 temp.add(new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)); 069 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 070 } 071 072 /** Creates a new instance of EditCancellationKindDescriptionCommand */ 073 public EditCancellationKindDescriptionCommand() { 074 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 075 } 076 077 @Override 078 protected BaseResult execute() { 079 var cancellationPolicyControl = Session.getModelController(CancellationPolicyControl.class); 080 var result = CancellationPolicyResultFactory.getEditCancellationKindDescriptionResult(); 081 var cancellationKindName = spec.getCancellationKindName(); 082 var cancellationKind = cancellationPolicyControl.getCancellationKindByName(cancellationKindName); 083 084 if(cancellationKind != null) { 085 var partyControl = Session.getModelController(PartyControl.class); 086 var languageIsoName = spec.getLanguageIsoName(); 087 var language = partyControl.getLanguageByIsoName(languageIsoName); 088 089 if(language != null) { 090 if(editMode.equals(EditMode.LOCK)) { 091 var cancellationKindDescription = cancellationPolicyControl.getCancellationKindDescription(cancellationKind, language); 092 093 if(cancellationKindDescription != null) { 094 result.setCancellationKindDescription(cancellationPolicyControl.getCancellationKindDescriptionTransfer(getUserVisit(), cancellationKindDescription)); 095 096 if(lockEntity(cancellationKind)) { 097 var edit = CancellationPolicyEditFactory.getCancellationKindDescriptionEdit(); 098 099 result.setEdit(edit); 100 edit.setDescription(cancellationKindDescription.getDescription()); 101 } else { 102 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 103 } 104 105 result.setEntityLock(getEntityLockTransfer(cancellationKind)); 106 } else { 107 addExecutionError(ExecutionErrors.UnknownCancellationKindDescription.name()); 108 } 109 } else if(editMode.equals(EditMode.UPDATE)) { 110 var cancellationKindDescriptionValue = cancellationPolicyControl.getCancellationKindDescriptionValueForUpdate(cancellationKind, language); 111 112 if(cancellationKindDescriptionValue != null) { 113 if(lockEntityForUpdate(cancellationKind)) { 114 try { 115 var description = edit.getDescription(); 116 117 cancellationKindDescriptionValue.setDescription(description); 118 119 cancellationPolicyControl.updateCancellationKindDescriptionFromValue(cancellationKindDescriptionValue, getPartyPK()); 120 } finally { 121 unlockEntity(cancellationKind); 122 } 123 } else { 124 addExecutionError(ExecutionErrors.EntityLockStale.name()); 125 } 126 } else { 127 addExecutionError(ExecutionErrors.UnknownCancellationKindDescription.name()); 128 } 129 } 130 } else { 131 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 132 } 133 } else { 134 addExecutionError(ExecutionErrors.UnknownCancellationKindName.name(), cancellationKindName); 135 } 136 137 return result; 138 } 139 140}