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.CreateContentCollectionForm; 020import com.echothree.control.user.content.common.result.ContentResultFactory; 021import com.echothree.model.control.content.common.ContentSections; 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.offer.server.entity.OfferUse; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.server.control.BaseSimpleCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import com.echothree.util.server.persistence.Session; 041import java.util.Arrays; 042import java.util.Collections; 043import java.util.List; 044import javax.enterprise.context.RequestScoped; 045 046@RequestScoped 047public class CreateContentCollectionCommand 048 extends BaseSimpleCommand<CreateContentCollectionForm> { 049 050 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 051 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 052 053 static { 054 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 057 new SecurityRoleDefinition(SecurityRoleGroups.ContentCollection.name(), SecurityRoles.Create.name()) 058 ))) 059 ))); 060 061 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 062 new FieldDefinition("ContentCollectionName", 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("Description", FieldType.STRING, false, 1L, 132L) 067 )); 068 } 069 070 /** Creates a new instance of CreateContentCollectionCommand */ 071 public CreateContentCollectionCommand() { 072 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 073 } 074 075 @Override 076 protected BaseResult execute() { 077 var result = ContentResultFactory.getCreateContentCollectionResult(); 078 var contentControl = Session.getModelController(ContentControl.class); 079 var contentCollectionName = form.getContentCollectionName(); 080 var contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 081 082 if(contentCollection == null) { 083 var offerControl = Session.getModelController(OfferControl.class); 084 var defaultOfferName = form.getDefaultOfferName(); 085 var defaultUseName = form.getDefaultUseName(); 086 var defaultSourceName = form.getDefaultSourceName(); 087 OfferUse defaultOfferUse = null; 088 089 if(defaultOfferName != null && defaultUseName != null && defaultSourceName == null) { 090 var defaultOffer = offerControl.getOfferByName(defaultOfferName); 091 092 if(defaultOffer != null) { 093 var useControl = Session.getModelController(UseControl.class); 094 var defaultUse = useControl.getUseByName(defaultUseName); 095 096 if(defaultUse != null) { 097 var offerUseControl = Session.getModelController(OfferUseControl.class); 098 defaultOfferUse = offerUseControl.getOfferUse(defaultOffer, defaultUse); 099 100 if(defaultOfferUse == null) { 101 addExecutionError(ExecutionErrors.UnknownDefaultOfferUse.name()); 102 } 103 } else { 104 addExecutionError(ExecutionErrors.UnknownDefaultUseName.name(), defaultUseName); 105 } 106 } else { 107 addExecutionError(ExecutionErrors.UnknownDefaultOfferName.name(), defaultOfferName); 108 } 109 } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName != null) { 110 var sourceControl = Session.getModelController(SourceControl.class); 111 var source = sourceControl.getSourceByName(defaultSourceName); 112 113 if(source != null) { 114 defaultOfferUse = source.getLastDetail().getOfferUse(); 115 } else { 116 addExecutionError(ExecutionErrors.UnknownDefaultSourceName.name(), defaultSourceName); 117 } 118 } else { 119 var sourceControl = Session.getModelController(SourceControl.class); 120 // If all three parameters are null, then try to get the default Source and use its OfferUse. 121 var source = sourceControl.getDefaultSource(); 122 123 if(source != null) { 124 defaultOfferUse = source.getLastDetail().getOfferUse(); 125 } else { 126 addExecutionError(ExecutionErrors.InvalidDefaultOfferOrSourceSpecification.name()); 127 } 128 } 129 130 if(defaultOfferUse != null) { 131 var description = form.getDescription(); 132 var partyPK = getPartyPK(); 133 134 contentCollection = contentControl.createContentCollection(contentCollectionName, defaultOfferUse, partyPK); 135 contentControl.createContentSection(contentCollection, ContentSections.ROOT.toString(), null, false, 0, partyPK); 136 137 if(description != null) { 138 var language = getPreferredLanguage(); 139 140 contentControl.createContentCollectionDescription(contentCollection, language, description, partyPK); 141 } 142 } 143 } else { 144 addExecutionError(ExecutionErrors.DuplicateContentCollectionName.name(), contentCollectionName); 145 } 146 147 if(contentCollection != null) { 148 result.setContentCollectionName(contentCollection.getLastDetail().getContentCollectionName()); 149 result.setEntityRef(contentCollection.getPrimaryKey().getEntityRef()); 150 } 151 152 return result; 153 } 154 155}