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 com.echothree.util.server.persistence.Session; 046import java.util.List; 047import javax.enterprise.context.Dependent; 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 /** Creates a new instance of EditContentCollectionCommand */ 079 public EditContentCollectionCommand() { 080 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 081 } 082 083 @Override 084 public EditContentCollectionResult getResult() { 085 return ContentResultFactory.getEditContentCollectionResult(); 086 } 087 088 @Override 089 public ContentCollectionEdit getEdit() { 090 return ContentEditFactory.getContentCollectionEdit(); 091 } 092 093 @Override 094 public ContentCollection getEntity(EditContentCollectionResult result) { 095 var contentControl = Session.getModelController(ContentControl.class); 096 ContentCollection contentCollection; 097 var contentCollectionName = spec.getContentCollectionName(); 098 099 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 100 contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 101 } else { // EditMode.UPDATE 102 contentCollection = contentControl.getContentCollectionByNameForUpdate(contentCollectionName); 103 } 104 105 if(contentCollection != null) { 106 result.setContentCollection(contentControl.getContentCollectionTransfer(getUserVisit(), contentCollection)); 107 } else { 108 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 109 } 110 111 return contentCollection; 112 } 113 114 @Override 115 public ContentCollection getLockEntity(ContentCollection contentCollection) { 116 return contentCollection; 117 } 118 119 @Override 120 public void fillInResult(EditContentCollectionResult result, ContentCollection contentCollection) { 121 var contentControl = Session.getModelController(ContentControl.class); 122 123 result.setContentCollection(contentControl.getContentCollectionTransfer(getUserVisit(), contentCollection)); 124 } 125 126 @Override 127 public void doLock(ContentCollectionEdit edit, ContentCollection contentCollection) { 128 var contentControl = Session.getModelController(ContentControl.class); 129 var sourceControl = Session.getModelController(SourceControl.class); 130 var contentCollectionDescription = contentControl.getContentCollectionDescription(contentCollection, getPreferredLanguage()); 131 var contentCollectionDetail = contentCollection.getLastDetail(); 132 var offerUse = contentCollectionDetail.getDefaultOfferUse(); 133 var defaultOfferUseDetail = offerUse.getLastDetail(); 134 var sources = sourceControl.getSourcesByOfferUse(offerUse); 135 136 edit.setContentCollectionName(contentCollectionDetail.getContentCollectionName()); 137 edit.setDefaultOfferName(defaultOfferUseDetail.getOffer().getLastDetail().getOfferName()); 138 edit.setDefaultUseName(defaultOfferUseDetail.getUse().getLastDetail().getUseName()); 139 edit.setDefaultSourceName(sources.iterator().next().getLastDetail().getSourceName()); 140 141 if(contentCollectionDescription != null) { 142 edit.setDescription(contentCollectionDescription.getDescription()); 143 } 144 } 145 146 OfferUse defaultOfferUse = null; 147 148 @Override 149 public void canUpdate(ContentCollection contentCollection) { 150 var contentControl = Session.getModelController(ContentControl.class); 151 var contentCollectionName = edit.getContentCollectionName(); 152 var duplicateContentCollection = contentControl.getContentCollectionByName(contentCollectionName); 153 154 if(duplicateContentCollection == null || contentCollection.equals(duplicateContentCollection)) { 155 var offerControl = Session.getModelController(OfferControl.class); 156 var defaultOfferName = edit.getDefaultOfferName(); 157 var defaultUseName = edit.getDefaultUseName(); 158 var defaultSourceName = edit.getDefaultSourceName(); 159 160 if(defaultOfferName != null && defaultUseName != null && defaultSourceName == null) { 161 var defaultOffer = offerControl.getOfferByName(defaultOfferName); 162 163 if(defaultOffer != null) { 164 var useControl = Session.getModelController(UseControl.class); 165 var defaultUse = useControl.getUseByName(defaultUseName); 166 167 if(defaultUse != null) { 168 var offerUseControl = Session.getModelController(OfferUseControl.class); 169 defaultOfferUse = offerUseControl.getOfferUse(defaultOffer, defaultUse); 170 171 if(defaultOfferUse == null) { 172 addExecutionError(ExecutionErrors.UnknownDefaultOfferUse.name()); 173 } 174 } else { 175 addExecutionError(ExecutionErrors.UnknownDefaultUseName.name(), defaultUseName); 176 } 177 } else { 178 addExecutionError(ExecutionErrors.UnknownDefaultOfferName.name(), defaultOfferName); 179 } 180 } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName != null) { 181 var sourceControl = Session.getModelController(SourceControl.class); 182 var source = sourceControl.getSourceByName(defaultSourceName); 183 184 if(source != null) { 185 defaultOfferUse = source.getLastDetail().getOfferUse(); 186 } else { 187 addExecutionError(ExecutionErrors.UnknownDefaultSourceName.name(), defaultSourceName); 188 } 189 } else { 190 var sourceControl = Session.getModelController(SourceControl.class); 191 // If all three parameters are null, then try to get the default Source and use its OfferUse. 192 var source = sourceControl.getDefaultSource(); 193 194 if(source != null) { 195 defaultOfferUse = source.getLastDetail().getOfferUse(); 196 } else { 197 addExecutionError(ExecutionErrors.InvalidDefaultOfferOrSourceSpecification.name()); 198 } 199 } 200 201 if(defaultOfferUse != null) { 202 var partyControl = Session.getModelController(PartyControl.class); 203 var defaultOffer = defaultOfferUse.getLastDetail().getOffer(); 204 var defaultPartyDepartment = partyControl.getPartyDepartment(defaultOffer.getLastDetail().getDepartmentParty()); 205 var defaultPartyDivision = partyControl.getPartyDivision(defaultPartyDepartment.getDivisionParty()); 206 var defaultPartyCompany = partyControl.getPartyCompany(defaultPartyDivision.getCompanyParty()); 207 208 var collectionOffer = contentCollection.getLastDetail().getDefaultOfferUse().getLastDetail().getOffer(); 209 var collectionPartyDepartment = partyControl.getPartyDepartment(collectionOffer.getLastDetail().getDepartmentParty()); 210 var collectionPartyDivision = partyControl.getPartyDivision(collectionPartyDepartment.getDivisionParty()); 211 var collectionPartyCompany = partyControl.getPartyCompany(collectionPartyDivision.getCompanyParty()); 212 213 if(!defaultPartyCompany.equals(collectionPartyCompany)) { 214 addExecutionError(ExecutionErrors.InvalidOfferCompany.name()); 215 } 216 } 217 } else { 218 addExecutionError(ExecutionErrors.DuplicateContentCollectionName.name(), contentCollectionName); 219 } 220 } 221 222 @Override 223 public void doUpdate(ContentCollection contentCollection) { 224 var contentControl = Session.getModelController(ContentControl.class); 225 var partyPK = getPartyPK(); 226 var contentCollectionDetailValue = contentControl.getContentCollectionDetailValueForUpdate(contentCollection); 227 var contentCollectionDescription = contentControl.getContentCollectionDescriptionForUpdate(contentCollection, getPreferredLanguage()); 228 var description = edit.getDescription(); 229 230 contentCollectionDetailValue.setContentCollectionName(edit.getContentCollectionName()); 231 contentCollectionDetailValue.setDefaultOfferUsePK(defaultOfferUse.getPrimaryKey()); 232 233 contentControl.updateContentCollectionFromValue(contentCollectionDetailValue, partyPK); 234 235 if(contentCollectionDescription == null && description != null) { 236 contentControl.createContentCollectionDescription(contentCollection, getPreferredLanguage(), description, partyPK); 237 } else if(contentCollectionDescription != null && description == null) { 238 contentControl.deleteContentCollectionDescription(contentCollectionDescription, partyPK); 239 } else if(contentCollectionDescription != null && description != null) { 240 var contentCollectionDescriptionValue = contentControl.getContentCollectionDescriptionValue(contentCollectionDescription); 241 242 contentCollectionDescriptionValue.setDescription(description); 243 contentControl.updateContentCollectionDescriptionFromValue(contentCollectionDescriptionValue, partyPK); 244 } 245 } 246 247}