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