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