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.form.CreateContentCatalogForm; 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.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 030import com.echothree.model.data.content.server.entity.ContentCatalog; 031import com.echothree.model.data.offer.server.entity.OfferUse; 032import com.echothree.model.data.user.common.pk.UserVisitPK; 033import com.echothree.util.common.command.BaseResult; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.validation.FieldDefinition; 036import com.echothree.util.common.validation.FieldType; 037import com.echothree.util.server.control.BaseSimpleCommand; 038import com.echothree.util.server.control.CommandSecurityDefinition; 039import com.echothree.util.server.control.PartyTypeDefinition; 040import com.echothree.util.server.control.SecurityRoleDefinition; 041import com.echothree.util.server.persistence.Session; 042import java.util.List; 043import javax.enterprise.context.Dependent; 044 045@Dependent 046public class CreateContentCatalogCommand 047 extends BaseSimpleCommand<CreateContentCatalogForm> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 056 new SecurityRoleDefinition(SecurityRoleGroups.ContentCatalog.name(), SecurityRoles.Create.name()) 057 )) 058 )); 059 060 FORM_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("DefaultOfferName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("DefaultUseName", FieldType.ENTITY_NAME, false, null, null), 065 new FieldDefinition("DefaultSourceName", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 067 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 068 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 069 ); 070 } 071 072 /** Creates a new instance of CreateContentCatalogCommand */ 073 public CreateContentCatalogCommand() { 074 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 075 } 076 077 @Override 078 protected BaseResult execute() { 079 var result = ContentResultFactory.getCreateContentCatalogResult(); 080 var contentControl = Session.getModelController(ContentControl.class); 081 var contentCollectionName = form.getContentCollectionName(); 082 var contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 083 ContentCatalog contentCatalog = null; 084 085 if(contentCollection != null) { 086 var contentCatalogName = form.getContentCatalogName(); 087 088 contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 089 090 if(contentCatalog == null) { 091 var defaultOfferName = form.getDefaultOfferName(); 092 var defaultUseName = form.getDefaultUseName(); 093 var defaultSourceName = form.getDefaultSourceName(); 094 OfferUse defaultOfferUse = null; 095 096 if(defaultOfferName != null && defaultUseName != null && defaultSourceName == null) { 097 var offerControl = Session.getModelController(OfferControl.class); 098 var defaultOffer = offerControl.getOfferByName(defaultOfferName); 099 100 if(defaultOffer != null) { 101 var useControl = Session.getModelController(UseControl.class); 102 var defaultUse = useControl.getUseByName(defaultUseName); 103 104 if(defaultUse != null) { 105 var offerUseControl = Session.getModelController(OfferUseControl.class); 106 defaultOfferUse = offerUseControl.getOfferUse(defaultOffer, defaultUse); 107 108 if(defaultOfferUse == null) { 109 addExecutionError(ExecutionErrors.UnknownDefaultOfferUse.name()); 110 } 111 } else { 112 addExecutionError(ExecutionErrors.UnknownDefaultUseName.name(), defaultUseName); 113 } 114 } else { 115 addExecutionError(ExecutionErrors.UnknownDefaultOfferName.name(), defaultOfferName); 116 } 117 } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName != null) { 118 var sourceControl = Session.getModelController(SourceControl.class); 119 var source = sourceControl.getSourceByName(defaultSourceName); 120 121 if(source != null) { 122 defaultOfferUse = source.getLastDetail().getOfferUse(); 123 } else { 124 addExecutionError(ExecutionErrors.UnknownDefaultSourceName.name(), defaultSourceName); 125 } 126 } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName == null) { 127 defaultOfferUse = contentCollection.getLastDetail().getDefaultOfferUse(); 128 } else { 129 addExecutionError(ExecutionErrors.InvalidDefaultOfferOrSourceSpecification.name()); 130 } 131 132 if(defaultOfferUse != null) { 133 var isDefault = Boolean.valueOf(form.getIsDefault()); 134 var sortOrder = Integer.valueOf(form.getSortOrder()); 135 var description = form.getDescription(); 136 var partyPK = getPartyPK(); 137 138 contentCatalog = contentControl.createContentCatalog(contentCollection, contentCatalogName, defaultOfferUse, 139 isDefault, sortOrder, partyPK); 140 contentControl.createContentCategory(contentCatalog, ContentCategories.ROOT.toString(), null, 141 defaultOfferUse, null, false, 0, partyPK); 142 143 if(description != null) { 144 var language = getPreferredLanguage(); 145 146 contentControl.createContentCatalogDescription(contentCatalog, language, description, partyPK); 147 } 148 } 149 } else { 150 addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCatalogName); 151 } 152 } else { 153 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 154 } 155 156 if(contentCatalog != null) { 157 var contentCatalogDetail = contentCatalog.getLastDetail(); 158 159 result.setContentCollectionName(contentCatalogDetail.getContentCollection().getLastDetail().getContentCollectionName()); 160 result.setContentCatalogName(contentCatalogDetail.getContentCatalogName()); 161 result.setEntityRef(contentCatalog.getPrimaryKey().getEntityRef()); 162 } 163 164 return result; 165 } 166 167}