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