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.form.CreateContentCategoryForm; 020import com.echothree.control.user.content.common.result.ContentResultFactory; 021import com.echothree.model.control.content.common.ContentCategories; 022import com.echothree.model.control.content.server.control.ContentControl; 023import com.echothree.model.control.offer.server.control.OfferControl; 024import com.echothree.model.control.offer.server.control.OfferUseControl; 025import com.echothree.model.control.offer.server.control.SourceControl; 026import com.echothree.model.control.offer.server.control.UseControl; 027import com.echothree.model.control.party.common.PartyTypes; 028import com.echothree.model.control.party.server.control.PartyControl; 029import com.echothree.model.control.security.common.SecurityRoleGroups; 030import com.echothree.model.control.security.common.SecurityRoles; 031import com.echothree.model.control.selector.common.SelectorKinds; 032import com.echothree.model.control.selector.common.SelectorTypes; 033import com.echothree.model.control.selector.server.control.SelectorControl; 034import com.echothree.model.data.content.server.entity.ContentCategory; 035import com.echothree.model.data.offer.server.entity.OfferUse; 036import com.echothree.model.data.selector.server.entity.Selector; 037import com.echothree.model.data.user.common.pk.UserVisitPK; 038import com.echothree.util.common.command.BaseResult; 039import com.echothree.util.common.message.ExecutionErrors; 040import com.echothree.util.common.validation.FieldDefinition; 041import com.echothree.util.common.validation.FieldType; 042import com.echothree.util.server.control.BaseSimpleCommand; 043import com.echothree.util.server.control.CommandSecurityDefinition; 044import com.echothree.util.server.control.PartyTypeDefinition; 045import com.echothree.util.server.control.SecurityRoleDefinition; 046import com.echothree.util.server.persistence.Session; 047import java.util.Arrays; 048import java.util.Collections; 049import java.util.List; 050import javax.enterprise.context.RequestScoped; 051 052@RequestScoped 053public class CreateContentCategoryCommand 054 extends BaseSimpleCommand<CreateContentCategoryForm> { 055 056 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 057 private final static List<FieldDefinition> FORM_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.ContentCategory.name(), SecurityRoles.Create.name()) 064 ))) 065 ))); 066 067 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 068 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null), 070 new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, true, null, null), 071 new FieldDefinition("ParentContentCategoryName", FieldType.ENTITY_NAME, false, null, null), 072 new FieldDefinition("DefaultOfferName", FieldType.ENTITY_NAME, false, null, null), 073 new FieldDefinition("DefaultUseName", FieldType.ENTITY_NAME, false, null, null), 074 new FieldDefinition("DefaultSourceName", FieldType.ENTITY_NAME, false, null, null), 075 new FieldDefinition("ContentCategoryItemSelectorName", FieldType.ENTITY_NAME, false, null, null), 076 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 077 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 078 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 079 )); 080 } 081 082 /** Creates a new instance of CreateContentCategoryCommand */ 083 public CreateContentCategoryCommand() { 084 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 085 } 086 087 @Override 088 protected BaseResult execute() { 089 var result = ContentResultFactory.getCreateContentCategoryResult(); 090 var contentControl = Session.getModelController(ContentControl.class); 091 var contentCollectionName = form.getContentCollectionName(); 092 var contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 093 ContentCategory contentCategory = null; 094 095 if(contentCollection != null) { 096 var contentCatalogName = form.getContentCatalogName(); 097 var contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 098 099 if(contentCatalog != null) { 100 var contentCategoryName = form.getContentCategoryName(); 101 102 contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName); 103 104 if(contentCategory == null) { 105 var parentContentCategoryName = form.getParentContentCategoryName(); 106 107 if(parentContentCategoryName == null) 108 parentContentCategoryName = ContentCategories.ROOT.toString(); 109 110 var parentContentCategory = parentContentCategoryName == null? null: contentControl.getContentCategoryByName(contentCatalog, 111 parentContentCategoryName); 112 113 if(parentContentCategory == null) { 114 addExecutionError(ExecutionErrors.UnknownParentContentCategoryName.name(), parentContentCategoryName); 115 } else { 116 var offerControl = Session.getModelController(OfferControl.class); 117 var defaultOfferName = form.getDefaultOfferName(); 118 var defaultUseName = form.getDefaultUseName(); 119 var defaultSourceName = form.getDefaultSourceName(); 120 OfferUse defaultOfferUse = null; 121 var invalidDefaultOfferOrSourceSpecification = false; 122 123 if(defaultOfferName != null && defaultUseName != null && defaultSourceName == null) { 124 var defaultOffer = offerControl.getOfferByName(defaultOfferName); 125 126 if(defaultOffer != null) { 127 var useControl = Session.getModelController(UseControl.class); 128 var defaultUse = useControl.getUseByName(defaultUseName); 129 130 if(defaultUse != null) { 131 var offerUseControl = Session.getModelController(OfferUseControl.class); 132 defaultOfferUse = offerUseControl.getOfferUse(defaultOffer, defaultUse); 133 134 if(defaultOfferUse == null) { 135 addExecutionError(ExecutionErrors.UnknownDefaultOfferUse.name()); 136 } 137 } else { 138 addExecutionError(ExecutionErrors.UnknownDefaultUseName.name(), defaultUseName); 139 } 140 } else { 141 addExecutionError(ExecutionErrors.UnknownDefaultOfferName.name(), defaultOfferName); 142 } 143 } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName != null) { 144 var sourceControl = Session.getModelController(SourceControl.class); 145 var source = sourceControl.getSourceByName(defaultSourceName); 146 147 if(source != null) { 148 defaultOfferUse = source.getLastDetail().getOfferUse(); 149 } else { 150 addExecutionError(ExecutionErrors.UnknownDefaultSourceName.name(), defaultSourceName); 151 } 152 } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName == null) { 153 // nothing 154 } else { 155 addExecutionError(ExecutionErrors.InvalidDefaultOfferOrSourceSpecification.name()); 156 invalidDefaultOfferOrSourceSpecification = true; 157 } 158 159 if(!invalidDefaultOfferOrSourceSpecification) { 160 var invalidOfferCompany = false; 161 162 if(defaultOfferUse != null) { 163 var partyControl = Session.getModelController(PartyControl.class); 164 var defaultOffer = defaultOfferUse.getLastDetail().getOffer(); 165 var defaultPartyDepartment = partyControl.getPartyDepartment(defaultOffer.getLastDetail().getDepartmentParty()); 166 var defaultPartyDivision = partyControl.getPartyDivision(defaultPartyDepartment.getDivisionParty()); 167 var defaultPartyCompany = partyControl.getPartyCompany(defaultPartyDivision.getCompanyParty()); 168 169 var catalogOffer = contentCatalog.getLastDetail().getDefaultOfferUse().getLastDetail().getOffer(); 170 var catalogPartyDepartment = partyControl.getPartyDepartment(catalogOffer.getLastDetail().getDepartmentParty()); 171 var catalogPartyDivision = partyControl.getPartyDivision(catalogPartyDepartment.getDivisionParty()); 172 var catalogPartyCompany = partyControl.getPartyCompany(catalogPartyDivision.getCompanyParty()); 173 174 if(!defaultPartyCompany.equals(catalogPartyCompany)) { 175 invalidOfferCompany = true; 176 } 177 } 178 179 if(!invalidOfferCompany) { 180 var contentCategoryItemSelectorName = form.getContentCategoryItemSelectorName(); 181 Selector contentCategoryItemSelector = null; 182 183 if(contentCategoryItemSelectorName != null) { 184 var selectorControl = Session.getModelController(SelectorControl.class); 185 var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name()); 186 187 if(selectorKind != null) { 188 var selectorType = selectorControl.getSelectorTypeByName(selectorKind, SelectorTypes.CONTENT_CATEGORY.name()); 189 190 if(selectorType != null) { 191 contentCategoryItemSelector = selectorControl.getSelectorByName(selectorType, contentCategoryItemSelectorName); 192 } else { 193 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorTypes.CONTENT_CATEGORY.name()); 194 } 195 } else { 196 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name()); 197 } 198 } 199 200 if(contentCategoryItemSelectorName != null && contentCategoryItemSelector == null) { 201 addExecutionError(ExecutionErrors.UnknownContentCategoryItemSelectorName.name(), contentCategoryItemSelectorName); 202 } else { 203 var isDefault = Boolean.valueOf(form.getIsDefault()); 204 var sortOrder = Integer.valueOf(form.getSortOrder()); 205 var description = form.getDescription(); 206 var createdBy = getPartyPK(); 207 208 contentCategory = contentControl.createContentCategory(contentCatalog, contentCategoryName, 209 parentContentCategory, defaultOfferUse, contentCategoryItemSelector, isDefault, 210 sortOrder, createdBy); 211 212 if(description != null) { 213 contentControl.createContentCategoryDescription(contentCategory, getPreferredLanguage(), 214 description, createdBy); 215 } 216 } 217 } else { 218 addExecutionError(ExecutionErrors.InvalidOfferCompany.name()); 219 } 220 } 221 } 222 } else { 223 addExecutionError(ExecutionErrors.DuplicateContentCategoryName.name(), contentCategoryName); 224 } 225 } else { 226 addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCatalogName); 227 } 228 } else { 229 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 230 } 231 232 if(contentCategory != null) { 233 var contentCategoryDetail = contentCategory.getLastDetail(); 234 var contentCatalogDetail = contentCategoryDetail.getContentCatalog().getLastDetail(); 235 236 result.setContentCollectionName(contentCatalogDetail.getContentCollection().getLastDetail().getContentCollectionName()); 237 result.setContentCatalogName(contentCatalogDetail.getContentCatalogName()); 238 result.setContentCategoryName(contentCategoryDetail.getContentCategoryName()); 239 result.setEntityRef(contentCategory.getPrimaryKey().getEntityRef()); 240 } 241 242 return result; 243 } 244 245}