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