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.security.server.command; 018 019import com.echothree.control.user.security.common.edit.SecurityEditFactory; 020import com.echothree.control.user.security.common.edit.SecurityRoleDescriptionEdit; 021import com.echothree.control.user.security.common.form.EditSecurityRoleDescriptionForm; 022import com.echothree.control.user.security.common.result.EditSecurityRoleDescriptionResult; 023import com.echothree.control.user.security.common.result.SecurityResultFactory; 024import com.echothree.control.user.security.common.spec.SecurityRoleDescriptionSpec; 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.control.security.server.control.SecurityControl; 030import com.echothree.model.data.security.server.entity.SecurityRole; 031import com.echothree.model.data.security.server.entity.SecurityRoleDescription; 032import com.echothree.model.data.security.server.entity.SecurityRoleGroup; 033import com.echothree.model.data.user.common.pk.UserVisitPK; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.validation.FieldDefinition; 036import com.echothree.util.common.validation.FieldType; 037import com.echothree.util.common.command.EditMode; 038import com.echothree.util.server.control.BaseAbstractEditCommand; 039import com.echothree.util.server.control.CommandSecurityDefinition; 040import com.echothree.util.server.control.PartyTypeDefinition; 041import com.echothree.util.server.control.SecurityRoleDefinition; 042import com.echothree.util.server.persistence.Session; 043import java.util.List; 044import javax.enterprise.context.Dependent; 045 046@Dependent 047public class EditSecurityRoleDescriptionCommand 048 extends BaseAbstractEditCommand<SecurityRoleDescriptionSpec, SecurityRoleDescriptionEdit, EditSecurityRoleDescriptionResult, SecurityRoleDescription, SecurityRole> { 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(List.of( 056 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 057 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 058 new SecurityRoleDefinition(SecurityRoleGroups.SecurityRole.name(), SecurityRoles.Description.name()) 059 )) 060 )); 061 062 SPEC_FIELD_DEFINITIONS = List.of( 063 new FieldDefinition("SecurityRoleGroupName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("SecurityRoleName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 066 ); 067 068 EDIT_FIELD_DEFINITIONS = List.of( 069 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L) 070 ); 071 } 072 073 /** Creates a new instance of EditSecurityRoleDescriptionCommand */ 074 public EditSecurityRoleDescriptionCommand() { 075 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 076 } 077 078 @Override 079 public EditSecurityRoleDescriptionResult getResult() { 080 return SecurityResultFactory.getEditSecurityRoleDescriptionResult(); 081 } 082 083 @Override 084 public SecurityRoleDescriptionEdit getEdit() { 085 return SecurityEditFactory.getSecurityRoleDescriptionEdit(); 086 } 087 088 SecurityRoleGroup securityRoleGroup; 089 090 @Override 091 public SecurityRoleDescription getEntity(EditSecurityRoleDescriptionResult result) { 092 var securityControl = Session.getModelController(SecurityControl.class); 093 SecurityRoleDescription securityRoleDescription = null; 094 var securityRoleGroupName = spec.getSecurityRoleGroupName(); 095 096 securityRoleGroup = securityControl.getSecurityRoleGroupByName(securityRoleGroupName); 097 098 if(securityRoleGroup != null) { 099 var securityRoleName = spec.getSecurityRoleName(); 100 var securityRole = securityControl.getSecurityRoleByName(securityRoleGroup, securityRoleName); 101 102 if(securityRole != null) { 103 var partyControl = Session.getModelController(PartyControl.class); 104 var languageIsoName = spec.getLanguageIsoName(); 105 var language = partyControl.getLanguageByIsoName(languageIsoName); 106 107 if(language != null) { 108 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 109 securityRoleDescription = securityControl.getSecurityRoleDescription(securityRole, language); 110 } else { // EditMode.UPDATE 111 securityRoleDescription = securityControl.getSecurityRoleDescriptionForUpdate(securityRole, language); 112 } 113 114 if(securityRoleDescription == null) { 115 addExecutionError(ExecutionErrors.UnknownSecurityRoleDescription.name(), securityRoleGroupName, securityRoleName, languageIsoName); 116 } 117 } else { 118 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 119 } 120 } else { 121 addExecutionError(ExecutionErrors.UnknownSecurityRoleName.name(), securityRoleGroupName, securityRoleName); 122 } 123 } else { 124 addExecutionError(ExecutionErrors.UnknownSecurityRoleGroupName.name(), securityRoleGroupName); 125 } 126 127 return securityRoleDescription; 128 } 129 130 @Override 131 public SecurityRole getLockEntity(SecurityRoleDescription securityRoleDescription) { 132 return securityRoleDescription.getSecurityRole(); 133 } 134 135 @Override 136 public void fillInResult(EditSecurityRoleDescriptionResult result, SecurityRoleDescription securityRoleDescription) { 137 var securityControl = Session.getModelController(SecurityControl.class); 138 139 result.setSecurityRoleDescription(securityControl.getSecurityRoleDescriptionTransfer(getUserVisit(), securityRoleDescription)); 140 } 141 142 @Override 143 public void doLock(SecurityRoleDescriptionEdit edit, SecurityRoleDescription securityRoleDescription) { 144 edit.setDescription(securityRoleDescription.getDescription()); 145 } 146 147 @Override 148 public void doUpdate(SecurityRoleDescription securityRoleDescription) { 149 var securityControl = Session.getModelController(SecurityControl.class); 150 var securityRoleDescriptionValue = securityControl.getSecurityRoleDescriptionValue(securityRoleDescription); 151 152 securityRoleDescriptionValue.setDescription(edit.getDescription()); 153 154 securityControl.updateSecurityRoleDescriptionFromValue(securityRoleDescriptionValue, getPartyPK()); 155 } 156 157}