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.content.server.command; 018 019import com.echothree.control.user.content.common.edit.ContentEditFactory; 020import com.echothree.control.user.content.common.edit.ContentWebAddressEdit; 021import com.echothree.control.user.content.common.form.EditContentWebAddressForm; 022import com.echothree.control.user.content.common.result.ContentResultFactory; 023import com.echothree.control.user.content.common.result.EditContentWebAddressResult; 024import com.echothree.control.user.content.common.spec.ContentWebAddressSpec; 025import com.echothree.model.control.content.server.control.ContentControl; 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.content.server.entity.ContentCollection; 030import com.echothree.model.data.content.server.entity.ContentWebAddress; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.common.command.EditMode; 036import com.echothree.util.server.control.BaseAbstractEditCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import java.util.List; 041import javax.enterprise.context.Dependent; 042import javax.inject.Inject; 043 044@Dependent 045public class EditContentWebAddressCommand 046 extends BaseAbstractEditCommand<ContentWebAddressSpec, ContentWebAddressEdit, EditContentWebAddressResult, ContentWebAddress, ContentWebAddress> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 050 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 056 new SecurityRoleDefinition(SecurityRoleGroups.ContentWebAddress.name(), SecurityRoles.Edit.name()) 057 )) 058 )); 059 060 SPEC_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, true, null, null) 062 ); 063 064 EDIT_FIELD_DEFINITIONS = List.of( 065 new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, true, null, null), 066 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null), 067 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 068 ); 069 } 070 071 @Inject 072 ContentControl contentControl; 073 074 075 /** Creates a new instance of EditContentWebAddressCommand */ 076 public EditContentWebAddressCommand() { 077 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 public EditContentWebAddressResult getResult() { 082 return ContentResultFactory.getEditContentWebAddressResult(); 083 } 084 085 @Override 086 public ContentWebAddressEdit getEdit() { 087 return ContentEditFactory.getContentWebAddressEdit(); 088 } 089 090 @Override 091 public ContentWebAddress getEntity(EditContentWebAddressResult result) { 092 ContentWebAddress contentWebAddress; 093 var contentWebAddressName = spec.getContentWebAddressName(); 094 095 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 096 contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName); 097 } else { // EditMode.UPDATE 098 contentWebAddress = contentControl.getContentWebAddressByNameForUpdate(contentWebAddressName); 099 } 100 101 if(contentWebAddress != null) { 102 result.setContentWebAddress(contentControl.getContentWebAddressTransfer(getUserVisit(), contentWebAddress)); 103 } else { 104 addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName); 105 } 106 107 return contentWebAddress; 108 } 109 110 @Override 111 public ContentWebAddress getLockEntity(ContentWebAddress contentWebAddress) { 112 return contentWebAddress; 113 } 114 115 @Override 116 public void fillInResult(EditContentWebAddressResult result, ContentWebAddress contentWebAddress) { 117 result.setContentWebAddress(contentControl.getContentWebAddressTransfer(getUserVisit(), contentWebAddress)); 118 } 119 120 @Override 121 public void doLock(ContentWebAddressEdit edit, ContentWebAddress contentWebAddress) { 122 var contentWebAddressDescription = contentControl.getContentWebAddressDescription(contentWebAddress, getPreferredLanguage()); 123 var contentWebAddressDetail = contentWebAddress.getLastDetail(); 124 125 edit.setContentWebAddressName(contentWebAddressDetail.getContentWebAddressName()); 126 edit.setContentCollectionName(contentWebAddressDetail.getContentCollection().getLastDetail().getContentCollectionName()); 127 128 if(contentWebAddressDescription != null) { 129 edit.setDescription(contentWebAddressDescription.getDescription()); 130 } 131 } 132 133 ContentCollection contentCollection = null; 134 135 @Override 136 public void canUpdate(ContentWebAddress contentWebAddress) { 137 var contentWebAddressName = edit.getContentWebAddressName(); 138 var duplicateContentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName); 139 140 if(duplicateContentWebAddress == null || contentWebAddress.equals(duplicateContentWebAddress)) { 141 var contentCollectionName = edit.getContentCollectionName(); 142 143 contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 144 145 if(contentCollection == null) { 146 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 147 } 148 } else { 149 addExecutionError(ExecutionErrors.DuplicateContentWebAddressName.name(), contentWebAddressName); 150 } 151 } 152 153 @Override 154 public void doUpdate(ContentWebAddress contentWebAddress) { 155 var partyPK = getPartyPK(); 156 var contentWebAddressDetailValue = contentControl.getContentWebAddressDetailValueForUpdate(contentWebAddress); 157 var contentWebAddressDescription = contentControl.getContentWebAddressDescriptionForUpdate(contentWebAddress, getPreferredLanguage()); 158 var description = edit.getDescription(); 159 160 contentWebAddressDetailValue.setContentWebAddressName(edit.getContentWebAddressName()); 161 contentWebAddressDetailValue.setContentCollectionPK(contentCollection.getPrimaryKey()); 162 163 contentControl.updateContentWebAddressFromValue(contentWebAddressDetailValue, partyPK); 164 165 if(contentWebAddressDescription == null && description != null) { 166 contentControl.createContentWebAddressDescription(contentWebAddress, getPreferredLanguage(), description, partyPK); 167 } else if(contentWebAddressDescription != null && description == null) { 168 contentControl.deleteContentWebAddressDescription(contentWebAddressDescription, partyPK); 169 } else if(contentWebAddressDescription != null && description != null) { 170 var contentWebAddressDescriptionValue = contentControl.getContentWebAddressDescriptionValue(contentWebAddressDescription); 171 172 contentWebAddressDescriptionValue.setDescription(description); 173 contentControl.updateContentWebAddressDescriptionFromValue(contentWebAddressDescriptionValue, partyPK); 174 } 175 } 176 177}