001 002// -------------------------------------------------------------------------------- 003// Copyright 2002-2024 Echo Three, LLC 004// 005// Licensed under the Apache License, Version 2.0 (the "License"); 006// you may not use this file except in compliance with the License. 007// You may obtain a copy of the License at 008// 009// http://www.apache.org/licenses/LICENSE-2.0 010// 011// Unless required by applicable law or agreed to in writing, software 012// distributed under the License is distributed on an "AS IS" BASIS, 013// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014// See the License for the specific language governing permissions and 015// limitations under the License. 016// -------------------------------------------------------------------------------- 017 018package com.echothree.control.user.sequence.server.command; 019 020import com.echothree.control.user.sequence.common.form.DeleteSequenceForm; 021import com.echothree.model.control.party.common.PartyTypes; 022import com.echothree.model.control.security.common.SecurityRoleGroups; 023import com.echothree.model.control.security.common.SecurityRoles; 024import com.echothree.model.control.sequence.server.control.SequenceControl; 025import com.echothree.model.data.sequence.server.entity.Sequence; 026import com.echothree.model.data.sequence.server.entity.SequenceType; 027import com.echothree.model.data.user.common.pk.UserVisitPK; 028import com.echothree.util.common.message.ExecutionErrors; 029import com.echothree.util.common.validation.FieldDefinition; 030import com.echothree.util.common.validation.FieldType; 031import com.echothree.util.common.command.BaseResult; 032import com.echothree.util.server.control.BaseSimpleCommand; 033import com.echothree.util.server.control.CommandSecurityDefinition; 034import com.echothree.util.server.control.PartyTypeDefinition; 035import com.echothree.util.server.control.SecurityRoleDefinition; 036import com.echothree.util.server.persistence.Session; 037import java.util.Arrays; 038import java.util.Collections; 039import java.util.List; 040 041public class DeleteSequenceCommand 042 extends BaseSimpleCommand<DeleteSequenceForm> { 043 044 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 045 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 046 047 static { 048 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 049 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 050 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 051 new SecurityRoleDefinition(SecurityRoleGroups.Sequence.name(), SecurityRoles.Delete.name()) 052 ))) 053 ))); 054 055 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 056 new FieldDefinition("SequenceTypeName", FieldType.ENTITY_NAME, true, null, null), 057 new FieldDefinition("SequenceName", FieldType.ENTITY_NAME, true, null, null) 058 )); 059 } 060 061 /** Creates a new instance of DeleteSequenceCommand */ 062 public DeleteSequenceCommand(UserVisitPK userVisitPK, DeleteSequenceForm form) { 063 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 064 } 065 066 @Override 067 protected BaseResult execute() { 068 var sequenceControl = Session.getModelController(SequenceControl.class); 069 String sequenceTypeName = form.getSequenceTypeName(); 070 SequenceType sequenceType = sequenceControl.getSequenceTypeByName(sequenceTypeName); 071 072 if(sequenceType != null) { 073 String sequenceName = form.getSequenceName(); 074 Sequence sequence = sequenceControl.getSequenceByNameForUpdate(sequenceType, sequenceName); 075 076 if(sequence != null) { 077 sequenceControl.deleteSequence(sequence, getPartyPK()); 078 } else { 079 addExecutionError(ExecutionErrors.UnknownSequenceName.name(), sequenceName); 080 } 081 } else { 082 addExecutionError(ExecutionErrors.UnknownSequenceTypeName.name(), sequenceTypeName); 083 } 084 085 return null; 086 } 087 088}