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 com.echothree.util.server.persistence.Session; 041import java.util.List; 042import javax.enterprise.context.Dependent; 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 /** Creates a new instance of EditContentWebAddressCommand */ 072 public EditContentWebAddressCommand() { 073 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 074 } 075 076 @Override 077 public EditContentWebAddressResult getResult() { 078 return ContentResultFactory.getEditContentWebAddressResult(); 079 } 080 081 @Override 082 public ContentWebAddressEdit getEdit() { 083 return ContentEditFactory.getContentWebAddressEdit(); 084 } 085 086 @Override 087 public ContentWebAddress getEntity(EditContentWebAddressResult result) { 088 var contentControl = Session.getModelController(ContentControl.class); 089 ContentWebAddress contentWebAddress; 090 var contentWebAddressName = spec.getContentWebAddressName(); 091 092 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 093 contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName); 094 } else { // EditMode.UPDATE 095 contentWebAddress = contentControl.getContentWebAddressByNameForUpdate(contentWebAddressName); 096 } 097 098 if(contentWebAddress != null) { 099 result.setContentWebAddress(contentControl.getContentWebAddressTransfer(getUserVisit(), contentWebAddress)); 100 } else { 101 addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName); 102 } 103 104 return contentWebAddress; 105 } 106 107 @Override 108 public ContentWebAddress getLockEntity(ContentWebAddress contentWebAddress) { 109 return contentWebAddress; 110 } 111 112 @Override 113 public void fillInResult(EditContentWebAddressResult result, ContentWebAddress contentWebAddress) { 114 var contentControl = Session.getModelController(ContentControl.class); 115 116 result.setContentWebAddress(contentControl.getContentWebAddressTransfer(getUserVisit(), contentWebAddress)); 117 } 118 119 @Override 120 public void doLock(ContentWebAddressEdit edit, ContentWebAddress contentWebAddress) { 121 var contentControl = Session.getModelController(ContentControl.class); 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 contentControl = Session.getModelController(ContentControl.class); 138 var contentWebAddressName = edit.getContentWebAddressName(); 139 var duplicateContentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName); 140 141 if(duplicateContentWebAddress == null || contentWebAddress.equals(duplicateContentWebAddress)) { 142 var contentCollectionName = edit.getContentCollectionName(); 143 144 contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 145 146 if(contentCollection == null) { 147 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 148 } 149 } else { 150 addExecutionError(ExecutionErrors.DuplicateContentWebAddressName.name(), contentWebAddressName); 151 } 152 } 153 154 @Override 155 public void doUpdate(ContentWebAddress contentWebAddress) { 156 var contentControl = Session.getModelController(ContentControl.class); 157 var partyPK = getPartyPK(); 158 var contentWebAddressDetailValue = contentControl.getContentWebAddressDetailValueForUpdate(contentWebAddress); 159 var contentWebAddressDescription = contentControl.getContentWebAddressDescriptionForUpdate(contentWebAddress, getPreferredLanguage()); 160 var description = edit.getDescription(); 161 162 contentWebAddressDetailValue.setContentWebAddressName(edit.getContentWebAddressName()); 163 contentWebAddressDetailValue.setContentCollectionPK(contentCollection.getPrimaryKey()); 164 165 contentControl.updateContentWebAddressFromValue(contentWebAddressDetailValue, partyPK); 166 167 if(contentWebAddressDescription == null && description != null) { 168 contentControl.createContentWebAddressDescription(contentWebAddress, getPreferredLanguage(), description, partyPK); 169 } else if(contentWebAddressDescription != null && description == null) { 170 contentControl.deleteContentWebAddressDescription(contentWebAddressDescription, partyPK); 171 } else if(contentWebAddressDescription != null && description != null) { 172 var contentWebAddressDescriptionValue = contentControl.getContentWebAddressDescriptionValue(contentWebAddressDescription); 173 174 contentWebAddressDescriptionValue.setDescription(description); 175 contentControl.updateContentWebAddressDescriptionFromValue(contentWebAddressDescriptionValue, partyPK); 176 } 177 } 178 179}