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.ContentCollectionEdit; 020import com.echothree.control.user.content.common.edit.ContentEditFactory; 021import com.echothree.control.user.content.common.form.EditContentCollectionForm; 022import com.echothree.control.user.content.common.result.ContentResultFactory; 023import com.echothree.control.user.content.common.result.EditContentCollectionResult; 024import com.echothree.control.user.content.common.spec.ContentCollectionSpec; 025import com.echothree.model.control.content.server.control.ContentControl; 026import com.echothree.model.control.offer.server.control.OfferControl; 027import com.echothree.model.control.offer.server.control.OfferUseControl; 028import com.echothree.model.control.offer.server.control.SourceControl; 029import com.echothree.model.control.offer.server.control.UseControl; 030import com.echothree.model.control.party.common.PartyTypes; 031import com.echothree.model.control.party.server.control.PartyControl; 032import com.echothree.model.control.security.common.SecurityRoleGroups; 033import com.echothree.model.control.security.common.SecurityRoles; 034import com.echothree.model.data.content.server.entity.ContentCollection; 035import com.echothree.model.data.offer.server.entity.OfferUse; 036import com.echothree.model.data.user.common.pk.UserVisitPK; 037import com.echothree.util.common.command.EditMode; 038import com.echothree.util.common.message.ExecutionErrors; 039import com.echothree.util.common.validation.FieldDefinition; 040import com.echothree.util.common.validation.FieldType; 041import com.echothree.util.server.control.BaseAbstractEditCommand; 042import com.echothree.util.server.control.CommandSecurityDefinition; 043import com.echothree.util.server.control.PartyTypeDefinition; 044import com.echothree.util.server.control.SecurityRoleDefinition; 045import java.util.List; 046import javax.enterprise.context.Dependent; 047import javax.inject.Inject; 048 049@Dependent 050public class EditContentCollectionCommand 051 extends BaseAbstractEditCommand<ContentCollectionSpec, ContentCollectionEdit, EditContentCollectionResult, ContentCollection, ContentCollection> { 052 053 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 054 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 055 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 056 057 static { 058 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 059 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 060 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 061 new SecurityRoleDefinition(SecurityRoleGroups.ContentCollection.name(), SecurityRoles.Edit.name()) 062 )) 063 )); 064 065 SPEC_FIELD_DEFINITIONS = List.of( 066 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null) 067 ); 068 069 EDIT_FIELD_DEFINITIONS = List.of( 070 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null), 071 new FieldDefinition("DefaultOfferName", FieldType.ENTITY_NAME, false, null, null), 072 new FieldDefinition("DefaultUseName", FieldType.ENTITY_NAME, false, null, null), 073 new FieldDefinition("DefaultSourceName", FieldType.ENTITY_NAME, false, null, null), 074 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 075 ); 076 } 077 078 @Inject 079 ContentControl contentControl; 080 081 @Inject 082 OfferControl offerControl; 083 084 @Inject 085 OfferUseControl offerUseControl; 086 087 @Inject 088 PartyControl partyControl; 089 090 @Inject 091 SourceControl sourceControl; 092 093 @Inject 094 UseControl useControl; 095 096 097 /** Creates a new instance of EditContentCollectionCommand */ 098 public EditContentCollectionCommand() { 099 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 100 } 101 102 @Override 103 public EditContentCollectionResult getResult() { 104 return ContentResultFactory.getEditContentCollectionResult(); 105 } 106 107 @Override 108 public ContentCollectionEdit getEdit() { 109 return ContentEditFactory.getContentCollectionEdit(); 110 } 111 112 @Override 113 public ContentCollection getEntity(EditContentCollectionResult result) { 114 ContentCollection contentCollection; 115 var contentCollectionName = spec.getContentCollectionName(); 116 117 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 118 contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 119 } else { // EditMode.UPDATE 120 contentCollection = contentControl.getContentCollectionByNameForUpdate(contentCollectionName); 121 } 122 123 if(contentCollection != null) { 124 result.setContentCollection(contentControl.getContentCollectionTransfer(getUserVisit(), contentCollection)); 125 } else { 126 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 127 } 128 129 return contentCollection; 130 } 131 132 @Override 133 public ContentCollection getLockEntity(ContentCollection contentCollection) { 134 return contentCollection; 135 } 136 137 @Override 138 public void fillInResult(EditContentCollectionResult result, ContentCollection contentCollection) { 139 result.setContentCollection(contentControl.getContentCollectionTransfer(getUserVisit(), contentCollection)); 140 } 141 142 @Override 143 public void doLock(ContentCollectionEdit edit, ContentCollection contentCollection) { 144 var contentCollectionDescription = contentControl.getContentCollectionDescription(contentCollection, getPreferredLanguage()); 145 var contentCollectionDetail = contentCollection.getLastDetail(); 146 var offerUse = contentCollectionDetail.getDefaultOfferUse(); 147 var defaultOfferUseDetail = offerUse.getLastDetail(); 148 var sources = sourceControl.getSourcesByOfferUse(offerUse); 149 150 edit.setContentCollectionName(contentCollectionDetail.getContentCollectionName()); 151 edit.setDefaultOfferName(defaultOfferUseDetail.getOffer().getLastDetail().getOfferName()); 152 edit.setDefaultUseName(defaultOfferUseDetail.getUse().getLastDetail().getUseName()); 153 edit.setDefaultSourceName(sources.iterator().next().getLastDetail().getSourceName()); 154 155 if(contentCollectionDescription != null) { 156 edit.setDescription(contentCollectionDescription.getDescription()); 157 } 158 } 159 160 OfferUse defaultOfferUse = null; 161 162 @Override 163 public void canUpdate(ContentCollection contentCollection) { 164 var contentCollectionName = edit.getContentCollectionName(); 165 var duplicateContentCollection = contentControl.getContentCollectionByName(contentCollectionName); 166 167 if(duplicateContentCollection == null || contentCollection.equals(duplicateContentCollection)) { 168 var defaultOfferName = edit.getDefaultOfferName(); 169 var defaultUseName = edit.getDefaultUseName(); 170 var defaultSourceName = edit.getDefaultSourceName(); 171 172 if(defaultOfferName != null && defaultUseName != null && defaultSourceName == null) { 173 var defaultOffer = offerControl.getOfferByName(defaultOfferName); 174 175 if(defaultOffer != null) { 176 var defaultUse = useControl.getUseByName(defaultUseName); 177 178 if(defaultUse != null) { 179 defaultOfferUse = offerUseControl.getOfferUse(defaultOffer, defaultUse); 180 181 if(defaultOfferUse == null) { 182 addExecutionError(ExecutionErrors.UnknownDefaultOfferUse.name()); 183 } 184 } else { 185 addExecutionError(ExecutionErrors.UnknownDefaultUseName.name(), defaultUseName); 186 } 187 } else { 188 addExecutionError(ExecutionErrors.UnknownDefaultOfferName.name(), defaultOfferName); 189 } 190 } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName != null) { 191 var source = sourceControl.getSourceByName(defaultSourceName); 192 193 if(source != null) { 194 defaultOfferUse = source.getLastDetail().getOfferUse(); 195 } else { 196 addExecutionError(ExecutionErrors.UnknownDefaultSourceName.name(), defaultSourceName); 197 } 198 } else { 199 // If all three parameters are null, then try to get the default Source and use its OfferUse. 200 var source = sourceControl.getDefaultSource(); 201 202 if(source != null) { 203 defaultOfferUse = source.getLastDetail().getOfferUse(); 204 } else { 205 addExecutionError(ExecutionErrors.InvalidDefaultOfferOrSourceSpecification.name()); 206 } 207 } 208 209 if(defaultOfferUse != null) { 210 var defaultOffer = defaultOfferUse.getLastDetail().getOffer(); 211 var defaultPartyDepartment = partyControl.getPartyDepartment(defaultOffer.getLastDetail().getDepartmentParty()); 212 var defaultPartyDivision = partyControl.getPartyDivision(defaultPartyDepartment.getDivisionParty()); 213 var defaultPartyCompany = partyControl.getPartyCompany(defaultPartyDivision.getCompanyParty()); 214 215 var collectionOffer = contentCollection.getLastDetail().getDefaultOfferUse().getLastDetail().getOffer(); 216 var collectionPartyDepartment = partyControl.getPartyDepartment(collectionOffer.getLastDetail().getDepartmentParty()); 217 var collectionPartyDivision = partyControl.getPartyDivision(collectionPartyDepartment.getDivisionParty()); 218 var collectionPartyCompany = partyControl.getPartyCompany(collectionPartyDivision.getCompanyParty()); 219 220 if(!defaultPartyCompany.equals(collectionPartyCompany)) { 221 addExecutionError(ExecutionErrors.InvalidOfferCompany.name()); 222 } 223 } 224 } else { 225 addExecutionError(ExecutionErrors.DuplicateContentCollectionName.name(), contentCollectionName); 226 } 227 } 228 229 @Override 230 public void doUpdate(ContentCollection contentCollection) { 231 var partyPK = getPartyPK(); 232 var contentCollectionDetailValue = contentControl.getContentCollectionDetailValueForUpdate(contentCollection); 233 var contentCollectionDescription = contentControl.getContentCollectionDescriptionForUpdate(contentCollection, getPreferredLanguage()); 234 var description = edit.getDescription(); 235 236 contentCollectionDetailValue.setContentCollectionName(edit.getContentCollectionName()); 237 contentCollectionDetailValue.setDefaultOfferUsePK(defaultOfferUse.getPrimaryKey()); 238 239 contentControl.updateContentCollectionFromValue(contentCollectionDetailValue, partyPK); 240 241 if(contentCollectionDescription == null && description != null) { 242 contentControl.createContentCollectionDescription(contentCollection, getPreferredLanguage(), description, partyPK); 243 } else if(contentCollectionDescription != null && description == null) { 244 contentControl.deleteContentCollectionDescription(contentCollectionDescription, partyPK); 245 } else if(contentCollectionDescription != null && description != null) { 246 var contentCollectionDescriptionValue = contentControl.getContentCollectionDescriptionValue(contentCollectionDescription); 247 248 contentCollectionDescriptionValue.setDescription(description); 249 contentControl.updateContentCollectionDescriptionFromValue(contentCollectionDescriptionValue, partyPK); 250 } 251 } 252 253}