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