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.edit.ContentCollectionEdit;
020import com.echothree.control.user.content.common.edit.ContentEditFactory;
021import com.echothree.control.user.content.common.form.EditContentCollectionForm;
022import com.echothree.control.user.content.common.result.ContentResultFactory;
023import com.echothree.control.user.content.common.result.EditContentCollectionResult;
024import com.echothree.control.user.content.common.spec.ContentCollectionSpec;
025import com.echothree.model.control.content.server.control.ContentControl;
026import com.echothree.model.control.offer.server.control.OfferControl;
027import com.echothree.model.control.offer.server.control.OfferUseControl;
028import com.echothree.model.control.offer.server.control.SourceControl;
029import com.echothree.model.control.offer.server.control.UseControl;
030import com.echothree.model.control.party.common.PartyTypes;
031import com.echothree.model.control.party.server.control.PartyControl;
032import com.echothree.model.control.security.common.SecurityRoleGroups;
033import com.echothree.model.control.security.common.SecurityRoles;
034import com.echothree.model.data.content.server.entity.ContentCollection;
035import com.echothree.model.data.content.server.entity.ContentCollectionDescription;
036import com.echothree.model.data.content.server.entity.ContentCollectionDetail;
037import com.echothree.model.data.content.server.value.ContentCollectionDescriptionValue;
038import com.echothree.model.data.content.server.value.ContentCollectionDetailValue;
039import com.echothree.model.data.offer.server.entity.Offer;
040import com.echothree.model.data.offer.server.entity.OfferUse;
041import com.echothree.model.data.offer.server.entity.OfferUseDetail;
042import com.echothree.model.data.offer.server.entity.Source;
043import com.echothree.model.data.offer.server.entity.Use;
044import com.echothree.model.data.party.server.entity.PartyCompany;
045import com.echothree.model.data.party.server.entity.PartyDepartment;
046import com.echothree.model.data.party.server.entity.PartyDivision;
047import com.echothree.model.data.user.common.pk.UserVisitPK;
048import com.echothree.util.common.command.EditMode;
049import com.echothree.util.common.message.ExecutionErrors;
050import com.echothree.util.common.validation.FieldDefinition;
051import com.echothree.util.common.validation.FieldType;
052import com.echothree.util.server.control.BaseAbstractEditCommand;
053import com.echothree.util.server.control.CommandSecurityDefinition;
054import com.echothree.util.server.control.PartyTypeDefinition;
055import com.echothree.util.server.control.SecurityRoleDefinition;
056import com.echothree.util.server.persistence.Session;
057import java.util.Arrays;
058import java.util.Collections;
059import java.util.List;
060
061public class EditContentCollectionCommand
062        extends BaseAbstractEditCommand<ContentCollectionSpec, ContentCollectionEdit, EditContentCollectionResult, ContentCollection, ContentCollection> {
063    
064    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
065    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
066    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
067    
068    static {
069        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
070                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
071                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
072                        new SecurityRoleDefinition(SecurityRoleGroups.ContentCollection.name(), SecurityRoles.Edit.name())
073                        )))
074                )));
075        
076        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
077                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null)
078                ));
079        
080        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
081                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
082                new FieldDefinition("DefaultOfferName", FieldType.ENTITY_NAME, false, null, null),
083                new FieldDefinition("DefaultUseName", FieldType.ENTITY_NAME, false, null, null),
084                new FieldDefinition("DefaultSourceName", FieldType.ENTITY_NAME, false, null, null),
085                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
086                ));
087    }
088    
089    /** Creates a new instance of EditContentCollectionCommand */
090    public EditContentCollectionCommand(UserVisitPK userVisitPK, EditContentCollectionForm form) {
091        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
092    }
093    
094    @Override
095    public EditContentCollectionResult getResult() {
096        return ContentResultFactory.getEditContentCollectionResult();
097    }
098    
099    @Override
100    public ContentCollectionEdit getEdit() {
101        return ContentEditFactory.getContentCollectionEdit();
102    }
103    
104    @Override
105    public ContentCollection getEntity(EditContentCollectionResult result) {
106        var contentControl = Session.getModelController(ContentControl.class);
107        ContentCollection contentCollection = null;
108        String contentCollectionName = spec.getContentCollectionName();
109
110        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
111            contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
112        } else { // EditMode.UPDATE
113            contentCollection = contentControl.getContentCollectionByNameForUpdate(contentCollectionName);
114        }
115
116        if(contentCollection != null) {
117            result.setContentCollection(contentControl.getContentCollectionTransfer(getUserVisit(), contentCollection));
118        } else {
119            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
120        }
121
122        return contentCollection;
123    }
124    
125    @Override
126    public ContentCollection getLockEntity(ContentCollection contentCollection) {
127        return contentCollection;
128    }
129    
130    @Override
131    public void fillInResult(EditContentCollectionResult result, ContentCollection contentCollection) {
132        var contentControl = Session.getModelController(ContentControl.class);
133        
134        result.setContentCollection(contentControl.getContentCollectionTransfer(getUserVisit(), contentCollection));
135    }
136    
137    @Override
138    public void doLock(ContentCollectionEdit edit, ContentCollection contentCollection) {
139        var contentControl = Session.getModelController(ContentControl.class);
140        var sourceControl = Session.getModelController(SourceControl.class);
141        ContentCollectionDescription contentCollectionDescription = contentControl.getContentCollectionDescription(contentCollection, getPreferredLanguage());
142        ContentCollectionDetail contentCollectionDetail = contentCollection.getLastDetail();
143        OfferUse offerUse = contentCollectionDetail.getDefaultOfferUse();
144        OfferUseDetail defaultOfferUseDetail = offerUse.getLastDetail();
145        List<Source> sources = sourceControl.getSourcesByOfferUse(offerUse);
146
147        edit.setContentCollectionName(contentCollectionDetail.getContentCollectionName());
148        edit.setDefaultOfferName(defaultOfferUseDetail.getOffer().getLastDetail().getOfferName());
149        edit.setDefaultUseName(defaultOfferUseDetail.getUse().getLastDetail().getUseName());
150        edit.setDefaultSourceName(sources.iterator().next().getLastDetail().getSourceName());
151
152        if(contentCollectionDescription != null) {
153            edit.setDescription(contentCollectionDescription.getDescription());
154        }
155    }
156    
157    OfferUse defaultOfferUse = null;
158    
159    @Override
160    public void canUpdate(ContentCollection contentCollection) {
161        var contentControl = Session.getModelController(ContentControl.class);
162        String contentCollectionName = edit.getContentCollectionName();
163        ContentCollection duplicateContentCollection = contentControl.getContentCollectionByName(contentCollectionName);
164
165        if(duplicateContentCollection == null || contentCollection.equals(duplicateContentCollection)) {
166            var offerControl = Session.getModelController(OfferControl.class);
167            String defaultOfferName = edit.getDefaultOfferName();
168            String defaultUseName = edit.getDefaultUseName();
169            String defaultSourceName = edit.getDefaultSourceName();
170            
171            if(defaultOfferName != null && defaultUseName != null && defaultSourceName == null) {
172                Offer defaultOffer = offerControl.getOfferByName(defaultOfferName);
173
174                if(defaultOffer != null) {
175                    var useControl = Session.getModelController(UseControl.class);
176                    Use defaultUse = useControl.getUseByName(defaultUseName);
177
178                    if(defaultUse != null) {
179                        var offerUseControl = Session.getModelController(OfferUseControl.class);
180                        defaultOfferUse = offerUseControl.getOfferUse(defaultOffer, defaultUse);
181
182                        if(defaultOfferUse == null) {
183                            addExecutionError(ExecutionErrors.UnknownDefaultOfferUse.name());
184                        }
185                    }  else {
186                        addExecutionError(ExecutionErrors.UnknownDefaultUseName.name(), defaultUseName);
187                    }
188                } else {
189                    addExecutionError(ExecutionErrors.UnknownDefaultOfferName.name(), defaultOfferName);
190                }
191            } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName != null) {
192                var sourceControl = Session.getModelController(SourceControl.class);
193                Source source = sourceControl.getSourceByName(defaultSourceName);
194
195                if(source != null) {
196                    defaultOfferUse = source.getLastDetail().getOfferUse();
197                } else {
198                    addExecutionError(ExecutionErrors.UnknownDefaultSourceName.name(), defaultSourceName);
199                }
200            } else {
201                var sourceControl = Session.getModelController(SourceControl.class);
202                // If all three parameters are null, then try to get the default Source and use its OfferUse.
203                Source source = sourceControl.getDefaultSource();
204
205                if(source != null) {
206                    defaultOfferUse = source.getLastDetail().getOfferUse();
207                } else {
208                    addExecutionError(ExecutionErrors.InvalidDefaultOfferOrSourceSpecification.name());
209                }
210            }
211
212            if(defaultOfferUse != null) {
213                var partyControl = Session.getModelController(PartyControl.class);
214                Offer defaultOffer = defaultOfferUse.getLastDetail().getOffer();
215                PartyDepartment defaultPartyDepartment = partyControl.getPartyDepartment(defaultOffer.getLastDetail().getDepartmentParty());
216                PartyDivision defaultPartyDivision = partyControl.getPartyDivision(defaultPartyDepartment.getDivisionParty());
217                PartyCompany defaultPartyCompany = partyControl.getPartyCompany(defaultPartyDivision.getCompanyParty());
218
219                Offer collectionOffer = contentCollection.getLastDetail().getDefaultOfferUse().getLastDetail().getOffer();
220                PartyDepartment collectionPartyDepartment = partyControl.getPartyDepartment(collectionOffer.getLastDetail().getDepartmentParty());
221                PartyDivision collectionPartyDivision = partyControl.getPartyDivision(collectionPartyDepartment.getDivisionParty());
222                PartyCompany collectionPartyCompany = partyControl.getPartyCompany(collectionPartyDivision.getCompanyParty());
223
224                if(!defaultPartyCompany.equals(collectionPartyCompany)) {
225                    addExecutionError(ExecutionErrors.InvalidOfferCompany.name());
226                }
227            }
228        } else {
229            addExecutionError(ExecutionErrors.DuplicateContentCollectionName.name(), contentCollectionName);
230        }
231    }
232    
233    @Override
234    public void doUpdate(ContentCollection contentCollection) {
235        var contentControl = Session.getModelController(ContentControl.class);
236        var partyPK = getPartyPK();
237        ContentCollectionDetailValue contentCollectionDetailValue = contentControl.getContentCollectionDetailValueForUpdate(contentCollection);
238        ContentCollectionDescription contentCollectionDescription = contentControl.getContentCollectionDescriptionForUpdate(contentCollection, getPreferredLanguage());
239        String description = edit.getDescription();
240
241        contentCollectionDetailValue.setContentCollectionName(edit.getContentCollectionName());
242        contentCollectionDetailValue.setDefaultOfferUsePK(defaultOfferUse.getPrimaryKey());
243
244        contentControl.updateContentCollectionFromValue(contentCollectionDetailValue, partyPK);
245
246        if(contentCollectionDescription == null && description != null) {
247            contentControl.createContentCollectionDescription(contentCollection, getPreferredLanguage(), description, partyPK);
248        } else if(contentCollectionDescription != null && description == null) {
249            contentControl.deleteContentCollectionDescription(contentCollectionDescription, partyPK);
250        } else if(contentCollectionDescription != null && description != null) {
251            ContentCollectionDescriptionValue contentCollectionDescriptionValue = contentControl.getContentCollectionDescriptionValue(contentCollectionDescription);
252
253            contentCollectionDescriptionValue.setDescription(description);
254            contentControl.updateContentCollectionDescriptionFromValue(contentCollectionDescriptionValue, partyPK);
255        }
256    }
257    
258}