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.ContentCategoryEdit; 020import com.echothree.control.user.content.common.edit.ContentEditFactory; 021import com.echothree.control.user.content.common.form.EditContentCategoryForm; 022import com.echothree.control.user.content.common.result.ContentResultFactory; 023import com.echothree.control.user.content.common.result.EditContentCategoryResult; 024import com.echothree.control.user.content.common.spec.ContentCategorySpec; 025import com.echothree.model.control.content.common.ContentCategories; 026import com.echothree.model.control.content.server.control.ContentControl; 027import com.echothree.model.control.offer.server.control.OfferControl; 028import com.echothree.model.control.offer.server.control.OfferUseControl; 029import com.echothree.model.control.offer.server.control.SourceControl; 030import com.echothree.model.control.offer.server.control.UseControl; 031import com.echothree.model.control.party.common.PartyTypes; 032import com.echothree.model.control.party.server.control.PartyControl; 033import com.echothree.model.control.security.common.SecurityRoleGroups; 034import com.echothree.model.control.security.common.SecurityRoles; 035import com.echothree.model.control.selector.common.SelectorKinds; 036import com.echothree.model.control.selector.common.SelectorTypes; 037import com.echothree.model.control.selector.server.control.SelectorControl; 038import com.echothree.model.data.content.server.entity.ContentCatalog; 039import com.echothree.model.data.content.server.entity.ContentCategory; 040import com.echothree.model.data.offer.server.entity.OfferUse; 041import com.echothree.model.data.selector.server.entity.Selector; 042import com.echothree.model.data.user.common.pk.UserVisitPK; 043import com.echothree.util.common.command.EditMode; 044import com.echothree.util.common.message.ExecutionErrors; 045import com.echothree.util.common.validation.FieldDefinition; 046import com.echothree.util.common.validation.FieldType; 047import com.echothree.util.server.control.BaseAbstractEditCommand; 048import com.echothree.util.server.control.CommandSecurityDefinition; 049import com.echothree.util.server.control.PartyTypeDefinition; 050import com.echothree.util.server.control.SecurityRoleDefinition; 051import java.util.List; 052import javax.enterprise.context.Dependent; 053import javax.inject.Inject; 054 055@Dependent 056public class EditContentCategoryCommand 057 extends BaseAbstractEditCommand<ContentCategorySpec, ContentCategoryEdit, EditContentCategoryResult, ContentCategory, ContentCategory> { 058 059 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 060 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 061 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 062 063 static { 064 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 065 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 066 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 067 new SecurityRoleDefinition(SecurityRoleGroups.ContentCategory.name(), SecurityRoles.Edit.name()) 068 )) 069 )); 070 071 SPEC_FIELD_DEFINITIONS = List.of( 072 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null), 073 new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null), 074 new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, true, null, null) 075 ); 076 077 EDIT_FIELD_DEFINITIONS = List.of( 078 new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, true, null, null), 079 new FieldDefinition("ParentContentCategoryName", FieldType.ENTITY_NAME, false, null, null), 080 new FieldDefinition("DefaultOfferName", FieldType.ENTITY_NAME, false, null, null), 081 new FieldDefinition("DefaultUseName", FieldType.ENTITY_NAME, false, null, null), 082 new FieldDefinition("DefaultSourceName", FieldType.ENTITY_NAME, false, null, null), 083 new FieldDefinition("ContentCategoryItemSelectorName", FieldType.ENTITY_NAME, false, null, null), 084 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 085 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 086 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 087 ); 088 } 089 090 @Inject 091 ContentControl contentControl; 092 093 @Inject 094 OfferControl offerControl; 095 096 @Inject 097 OfferUseControl offerUseControl; 098 099 @Inject 100 PartyControl partyControl; 101 102 @Inject 103 SelectorControl selectorControl; 104 105 @Inject 106 SourceControl sourceControl; 107 108 @Inject 109 UseControl useControl; 110 111 112 /** Creates a new instance of EditContentCategoryCommand */ 113 public EditContentCategoryCommand() { 114 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 115 } 116 117 @Override 118 public EditContentCategoryResult getResult() { 119 return ContentResultFactory.getEditContentCategoryResult(); 120 } 121 122 @Override 123 public ContentCategoryEdit getEdit() { 124 return ContentEditFactory.getContentCategoryEdit(); 125 } 126 127 ContentCatalog contentCatalog = null; 128 129 @Override 130 public ContentCategory getEntity(EditContentCategoryResult result) { 131 ContentCategory contentCategory = null; 132 var contentCollectionName = spec.getContentCollectionName(); 133 var contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 134 135 if(contentCollection != null) { 136 var contentCatalogName = spec.getContentCatalogName(); 137 138 contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 139 140 if(contentCatalog != null) { 141 var contentCategoryName = spec.getContentCategoryName(); 142 143 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 144 contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName); 145 } else { // EditMode.UPDATE 146 contentCategory = contentControl.getContentCategoryByNameForUpdate(contentCatalog, contentCategoryName); 147 } 148 149 if(contentCategory != null) { 150 result.setContentCategory(contentControl.getContentCategoryTransfer(getUserVisit(), contentCategory)); 151 } else { 152 addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(), contentCategoryName); 153 } 154 } else { 155 addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCatalogName); 156 } 157 } else { 158 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 159 } 160 161 return contentCategory; 162 } 163 164 @Override 165 public ContentCategory getLockEntity(ContentCategory contentCategory) { 166 return contentCategory; 167 } 168 169 @Override 170 public void fillInResult(EditContentCategoryResult result, ContentCategory contentCategory) { 171 result.setContentCategory(contentControl.getContentCategoryTransfer(getUserVisit(), contentCategory)); 172 } 173 174 @Override 175 public void doLock(ContentCategoryEdit edit, ContentCategory contentCategory) { 176 var contentCategoryDescription = contentControl.getContentCategoryDescription(contentCategory, getPreferredLanguage()); 177 var contentCategoryDetail = contentCategory.getLastDetail(); 178 var parentContentCategory = contentCategoryDetail.getParentContentCategory(); 179 var defaultOfferUse = contentCategoryDetail.getDefaultOfferUse(); 180 var defaultOfferUseDetail = defaultOfferUse == null ? null : defaultOfferUse.getLastDetail(); 181 var sources = defaultOfferUse == null ? null : sourceControl.getSourcesByOfferUse(defaultOfferUse); // List of Sources if there are any for the OfferUse 182 var defaultSourceName = sources == null ? null : sources.iterator().next().getLastDetail().getSourceName(); // From the List, the first available Source's SourceName 183 var contentCategoryItemSelector = contentCategoryDetail.getContentCategoryItemSelector(); 184 185 edit.setContentCategoryName(contentCategoryDetail.getContentCategoryName()); 186 edit.setParentContentCategoryName(parentContentCategory == null ? null : parentContentCategory.getLastDetail().getContentCategoryName()); 187 edit.setDefaultOfferName(defaultSourceName == null ? (defaultOfferUseDetail == null? null: defaultOfferUseDetail.getOffer().getLastDetail().getOfferName()) : null); 188 edit.setDefaultUseName(defaultSourceName == null ? (defaultOfferUseDetail == null ? null : defaultOfferUseDetail.getUse().getLastDetail().getUseName()) : null); 189 edit.setDefaultSourceName(defaultSourceName); 190 edit.setContentCategoryItemSelectorName(contentCategoryItemSelector == null? null: contentCategoryItemSelector.getLastDetail().getSelectorName()); 191 edit.setIsDefault(contentCategoryDetail.getIsDefault().toString()); 192 edit.setSortOrder(contentCategoryDetail.getSortOrder().toString()); 193 194 if(contentCategoryDescription != null) { 195 edit.setDescription(contentCategoryDescription.getDescription()); 196 } 197 } 198 199 ContentCategory parentContentCategory = null; 200 OfferUse defaultOfferUse = null; 201 Selector contentCategoryItemSelector = null; 202 203 @Override 204 public void canUpdate(ContentCategory contentCategory) { 205 var contentCategoryName = edit.getContentCategoryName(); 206 var duplicateContentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName); 207 208 if(duplicateContentCategory == null || contentCategory.equals(duplicateContentCategory)) { 209 var parentContentCategoryName = edit.getParentContentCategoryName(); 210 211 parentContentCategory = contentControl.getContentCategoryByName(contentCatalog, parentContentCategoryName == null ? 212 ContentCategories.ROOT.toString() : parentContentCategoryName); 213 214 if(parentContentCategory != null) { 215 if(contentControl.isParentContentCategorySafe(contentCategory, parentContentCategory)) { 216 var defaultOfferName = edit.getDefaultOfferName(); 217 var defaultUseName = edit.getDefaultUseName(); 218 var defaultSourceName = edit.getDefaultSourceName(); 219 var invalidDefaultOfferOrSourceSpecification = false; 220 221 if(defaultOfferName != null && defaultUseName != null && defaultSourceName == null) { 222 var defaultOffer = offerControl.getOfferByName(defaultOfferName); 223 224 if(defaultOffer != null) { 225 var defaultUse = useControl.getUseByName(defaultUseName); 226 227 if(defaultUse != null) { 228 defaultOfferUse = offerUseControl.getOfferUse(defaultOffer, defaultUse); 229 230 if(defaultOfferUse == null) { 231 addExecutionError(ExecutionErrors.UnknownDefaultOfferUse.name()); 232 } 233 } else { 234 addExecutionError(ExecutionErrors.UnknownDefaultUseName.name(), defaultUseName); 235 } 236 } else { 237 addExecutionError(ExecutionErrors.UnknownDefaultOfferName.name(), defaultOfferName); 238 } 239 } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName != null) { 240 var source = sourceControl.getSourceByName(defaultSourceName); 241 242 if(source != null) { 243 defaultOfferUse = source.getLastDetail().getOfferUse(); 244 } else { 245 addExecutionError(ExecutionErrors.UnknownDefaultSourceName.name(), defaultSourceName); 246 } 247 } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName == null) { 248 // nothing 249 } else { 250 addExecutionError(ExecutionErrors.InvalidDefaultOfferOrSourceSpecification.name()); 251 invalidDefaultOfferOrSourceSpecification = true; 252 } 253 254 if(!invalidDefaultOfferOrSourceSpecification) { 255 var invalidOfferCompany = false; 256 257 if(defaultOfferUse != null) { 258 var defaultOffer = defaultOfferUse.getLastDetail().getOffer(); 259 var defaultPartyDepartment = partyControl.getPartyDepartment(defaultOffer.getLastDetail().getDepartmentParty()); 260 var defaultPartyDivision = partyControl.getPartyDivision(defaultPartyDepartment.getDivisionParty()); 261 var defaultPartyCompany = partyControl.getPartyCompany(defaultPartyDivision.getCompanyParty()); 262 263 var catalogOffer = contentCatalog.getLastDetail().getDefaultOfferUse().getLastDetail().getOffer(); 264 var catalogPartyDepartment = partyControl.getPartyDepartment(catalogOffer.getLastDetail().getDepartmentParty()); 265 var catalogPartyDivision = partyControl.getPartyDivision(catalogPartyDepartment.getDivisionParty()); 266 var catalogPartyCompany = partyControl.getPartyCompany(catalogPartyDivision.getCompanyParty()); 267 268 if(!defaultPartyCompany.equals(catalogPartyCompany)) { 269 invalidOfferCompany = true; 270 } 271 } 272 273 if(!invalidOfferCompany) { 274 var contentCategoryItemSelectorName = edit.getContentCategoryItemSelectorName(); 275 276 if(contentCategoryItemSelectorName != null) { 277 var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name()); 278 279 if(selectorKind != null) { 280 var selectorType = selectorControl.getSelectorTypeByName(selectorKind, SelectorTypes.CONTENT_CATEGORY.name()); 281 282 if(selectorType != null) { 283 contentCategoryItemSelector = selectorControl.getSelectorByName(selectorType, contentCategoryItemSelectorName); 284 } else { 285 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorTypes.CONTENT_CATEGORY.name()); 286 } 287 } else { 288 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name()); 289 } 290 } 291 292 if(contentCategoryItemSelectorName != null && contentCategoryItemSelector == null) { 293 addExecutionError(ExecutionErrors.UnknownContentCategoryItemSelectorName.name(), contentCategoryItemSelectorName); 294 } 295 } else { 296 addExecutionError(ExecutionErrors.InvalidOfferCompany.name()); 297 } 298 } 299 } else { 300 addExecutionError(ExecutionErrors.InvalidParentContentCategory.name()); 301 } 302 } else { 303 addExecutionError(ExecutionErrors.UnknownParentContentCategoryName.name(), parentContentCategoryName); 304 } 305 } else { 306 addExecutionError(ExecutionErrors.DuplicateContentCategoryName.name(), contentCategoryName); 307 } 308 } 309 310 @Override 311 public void doUpdate(ContentCategory contentCategory) { 312 var partyPK = getPartyPK(); 313 var contentCategoryDetailValue = contentControl.getContentCategoryDetailValueForUpdate(contentCategory); 314 var contentCategoryDescription = contentControl.getContentCategoryDescriptionForUpdate(contentCategory, getPreferredLanguage()); 315 var description = edit.getDescription(); 316 317 contentCategoryDetailValue.setContentCategoryName(edit.getContentCategoryName()); 318 contentCategoryDetailValue.setParentContentCategoryPK(parentContentCategory == null ? null : parentContentCategory.getPrimaryKey()); 319 contentCategoryDetailValue.setDefaultOfferUsePK(defaultOfferUse == null? null: defaultOfferUse.getPrimaryKey()); 320 contentCategoryDetailValue.setContentCategoryItemSelectorPK(contentCategoryItemSelector == null? null: contentCategoryItemSelector.getPrimaryKey()); 321 contentCategoryDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 322 contentCategoryDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 323 324 contentControl.updateContentCategoryFromValue(contentCategoryDetailValue, partyPK); 325 326 if(contentCategoryDescription == null && description != null) { 327 contentControl.createContentCategoryDescription(contentCategory, getPreferredLanguage(), description, partyPK); 328 } else if(contentCategoryDescription != null && description == null) { 329 contentControl.deleteContentCategoryDescription(contentCategoryDescription, partyPK); 330 } else if(contentCategoryDescription != null && description != null) { 331 var contentCategoryDescriptionValue = contentControl.getContentCategoryDescriptionValue(contentCategoryDescription); 332 333 contentCategoryDescriptionValue.setDescription(description); 334 contentControl.updateContentCategoryDescriptionFromValue(contentCategoryDescriptionValue, partyPK); 335 } 336 } 337 338}