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.offer.server.command; 018 019import com.echothree.control.user.offer.common.edit.OfferEditFactory; 020import com.echothree.control.user.offer.common.edit.SourceEdit; 021import com.echothree.control.user.offer.common.form.EditSourceForm; 022import com.echothree.control.user.offer.common.result.EditSourceResult; 023import com.echothree.control.user.offer.common.result.OfferResultFactory; 024import com.echothree.control.user.offer.common.spec.SourceSpec; 025import com.echothree.model.control.offer.server.control.SourceControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.offer.server.entity.Source; 030import com.echothree.model.data.offer.server.entity.SourceDetail; 031import com.echothree.model.data.offer.server.value.SourceDetailValue; 032import com.echothree.model.data.user.common.pk.UserVisitPK; 033import com.echothree.util.common.command.EditMode; 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.server.control.BaseAbstractEditCommand; 038import com.echothree.util.server.control.CommandSecurityDefinition; 039import com.echothree.util.server.control.PartyTypeDefinition; 040import com.echothree.util.server.control.SecurityRoleDefinition; 041import com.echothree.util.server.persistence.Session; 042import java.util.List; 043 044public class EditSourceCommand 045 extends BaseAbstractEditCommand<SourceSpec, SourceEdit, EditSourceResult, Source, Source> { 046 047 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 048 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 049 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.Source.name(), SecurityRoles.Edit.name()) 056 )) 057 )); 058 059 SPEC_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("SourceName", FieldType.ENTITY_NAME, true, null, null) 061 ); 062 063 EDIT_FIELD_DEFINITIONS = List.of( 064 new FieldDefinition("SourceName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 066 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null) 067 ); 068 } 069 070 /** Creates a new instance of EditSourceCommand */ 071 public EditSourceCommand(UserVisitPK userVisitPK, EditSourceForm form) { 072 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 073 } 074 075 @Override 076 public EditSourceResult getResult() { 077 return OfferResultFactory.getEditSourceResult(); 078 } 079 080 @Override 081 public SourceEdit getEdit() { 082 return OfferEditFactory.getSourceEdit(); 083 } 084 085 @Override 086 public Source getEntity(EditSourceResult result) { 087 var sourceControl = Session.getModelController(SourceControl.class); 088 Source source; 089 String sourceName = spec.getSourceName(); 090 091 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 092 source = sourceControl.getSourceByName(sourceName); 093 } else { // EditMode.UPDATE 094 source = sourceControl.getSourceByNameForUpdate(sourceName); 095 } 096 097 if(source != null) { 098 result.setSource(sourceControl.getSourceTransfer(getUserVisit(), source)); 099 } else { 100 addExecutionError(ExecutionErrors.UnknownSourceName.name(), sourceName); 101 } 102 103 return source; 104 } 105 106 @Override 107 public Source getLockEntity(Source source) { 108 return source; 109 } 110 111 @Override 112 public void fillInResult(EditSourceResult result, Source source) { 113 var sourceControl = Session.getModelController(SourceControl.class); 114 115 result.setSource(sourceControl.getSourceTransfer(getUserVisit(), source)); 116 } 117 118 @Override 119 public void doLock(SourceEdit edit, Source source) { 120 SourceDetail sourceDetail = source.getLastDetail(); 121 122 edit.setSourceName(sourceDetail.getSourceName()); 123 edit.setIsDefault(sourceDetail.getIsDefault().toString()); 124 edit.setSortOrder(sourceDetail.getSortOrder().toString()); 125 } 126 127 @Override 128 public void canUpdate(Source source) { 129 var sourceControl = Session.getModelController(SourceControl.class); 130 String sourceName = edit.getSourceName(); 131 Source duplicateSource = sourceControl.getSourceByName(sourceName); 132 133 if(duplicateSource != null && !source.equals(duplicateSource)) { 134 addExecutionError(ExecutionErrors.DuplicateSourceName.name(), sourceName); 135 } 136 } 137 138 @Override 139 public void doUpdate(Source source) { 140 var sourceControl = Session.getModelController(SourceControl.class); 141 var partyPK = getPartyPK(); 142 SourceDetailValue sourceDetailValue = sourceControl.getSourceDetailValueForUpdate(source); 143 144 sourceDetailValue.setSourceName(edit.getSourceName()); 145 sourceDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 146 sourceDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 147 148 sourceControl.updateSourceFromValue(sourceDetailValue, partyPK); 149 } 150 151}