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.ContentEditFactory;
020import com.echothree.control.user.content.common.edit.ContentPageEdit;
021import com.echothree.control.user.content.common.form.EditContentPageForm;
022import com.echothree.control.user.content.common.result.ContentResultFactory;
023import com.echothree.control.user.content.common.result.EditContentPageResult;
024import com.echothree.control.user.content.common.spec.ContentPageSpec;
025import com.echothree.model.control.content.server.control.ContentControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.content.server.entity.ContentCollection;
030import com.echothree.model.data.content.server.entity.ContentPage;
031import com.echothree.model.data.content.server.entity.ContentPageDescription;
032import com.echothree.model.data.content.server.entity.ContentPageDetail;
033import com.echothree.model.data.content.server.entity.ContentPageLayout;
034import com.echothree.model.data.content.server.entity.ContentSection;
035import com.echothree.model.data.content.server.value.ContentPageDescriptionValue;
036import com.echothree.model.data.content.server.value.ContentPageDetailValue;
037import com.echothree.model.data.user.common.pk.UserVisitPK;
038import com.echothree.util.common.message.ExecutionErrors;
039import com.echothree.util.common.validation.FieldDefinition;
040import com.echothree.util.common.validation.FieldType;
041import com.echothree.util.common.command.EditMode;
042import com.echothree.util.server.control.BaseAbstractEditCommand;
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 EditContentPageCommand
052        extends BaseAbstractEditCommand<ContentPageSpec, ContentPageEdit, EditContentPageResult, ContentPage, ContentPage> {
053    
054    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
055    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
056    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
057    
058    static {
059        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
060                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
061                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
062                        new SecurityRoleDefinition(SecurityRoleGroups.ContentPage.name(), SecurityRoles.Edit.name())
063                        )))
064                )));
065        
066        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
067                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
068                new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("ContentPageName", FieldType.ENTITY_NAME, true, null, null)
070                ));
071        
072        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
073                new FieldDefinition("ContentPageName", FieldType.ENTITY_NAME, true, null, null),
074                new FieldDefinition("ContentPageLayoutName", FieldType.ENTITY_NAME, true, null, null),
075                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
076                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
077                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
078                ));
079    }
080    
081    /** Creates a new instance of EditContentPageCommand */
082    public EditContentPageCommand(UserVisitPK userVisitPK, EditContentPageForm form) {
083        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
084    }
085    
086    @Override
087    public EditContentPageResult getResult() {
088        return ContentResultFactory.getEditContentPageResult();
089    }
090    
091    @Override
092    public ContentPageEdit getEdit() {
093        return ContentEditFactory.getContentPageEdit();
094    }
095    
096    ContentSection contentSection = null;
097    
098    @Override
099    public ContentPage getEntity(EditContentPageResult result) {
100        var contentControl = Session.getModelController(ContentControl.class);
101        ContentPage contentPage = null;
102        String contentCollectionName = spec.getContentCollectionName();
103        ContentCollection contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
104        
105        if(contentCollection != null) {
106            String contentSectionName = spec.getContentSectionName();
107            
108            contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
109            
110            if(contentSection != null) {
111                String contentPageName = spec.getContentPageName();
112                
113                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
114                    contentPage = contentControl.getContentPageByName(contentSection, contentPageName);
115                } else { // EditMode.UPDATE
116                    contentPage = contentControl.getContentPageByNameForUpdate(contentSection, contentPageName);
117                }
118
119                if(contentPage != null) {
120                    result.setContentPage(contentControl.getContentPageTransfer(getUserVisit(), contentPage));
121                } else {
122                    addExecutionError(ExecutionErrors.UnknownContentPageName.name(), contentPageName);
123                }
124            } else {
125                addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName);
126            }
127        } else {
128            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
129        }
130
131        return contentPage;
132    }
133    
134    @Override
135    public ContentPage getLockEntity(ContentPage contentPage) {
136        return contentPage;
137    }
138    
139    @Override
140    public void fillInResult(EditContentPageResult result, ContentPage contentPage) {
141        var contentControl = Session.getModelController(ContentControl.class);
142        
143        result.setContentPage(contentControl.getContentPageTransfer(getUserVisit(), contentPage));
144    }
145    
146    @Override
147    public void doLock(ContentPageEdit edit, ContentPage contentPage) {
148        var contentControl = Session.getModelController(ContentControl.class);
149        ContentPageDescription contentPageDescription = contentControl.getContentPageDescription(contentPage, getPreferredLanguage());
150        ContentPageDetail contentPageDetail = contentPage.getLastDetail();
151
152        edit.setContentPageName(contentPageDetail.getContentPageName());
153        edit.setContentPageLayoutName(contentPageDetail.getContentPageLayout().getLastDetail().getContentPageLayoutName());
154        edit.setIsDefault(contentPageDetail.getIsDefault().toString());
155        edit.setSortOrder(contentPageDetail.getSortOrder().toString());
156
157        if(contentPageDescription != null) {
158            edit.setDescription(contentPageDescription.getDescription());
159        }
160    }
161    
162    ContentPageLayout contentPageLayout = null;
163    
164    @Override
165    public void canUpdate(ContentPage contentPage) {
166        var contentControl = Session.getModelController(ContentControl.class);
167        String contentPageName = edit.getContentPageName();
168        ContentPage duplicateContentPage = contentControl.getContentPageByName(contentSection, contentPageName);
169
170        if(duplicateContentPage == null || contentPage.equals(duplicateContentPage)) {
171            String contentPageLayoutName = edit.getContentPageLayoutName();
172            
173            contentPageLayout = contentControl.getContentPageLayoutByName(contentPageLayoutName);
174
175            if(contentPageLayout == null) {
176                addExecutionError(ExecutionErrors.UnknownContentPageLayoutName.name(), contentPageLayoutName);
177            }
178        } else {
179            addExecutionError(ExecutionErrors.DuplicateContentPageName.name(), contentPageName);
180        }
181    }
182    
183    @Override
184    public void doUpdate(ContentPage contentPage) {
185        var contentControl = Session.getModelController(ContentControl.class);
186        var partyPK = getPartyPK();
187        ContentPageDetailValue contentPageDetailValue = contentControl.getContentPageDetailValueForUpdate(contentPage);
188        ContentPageDescription contentPageDescription = contentControl.getContentPageDescriptionForUpdate(contentPage, getPreferredLanguage());
189        String description = edit.getDescription();
190
191        contentPageDetailValue.setContentPageName(edit.getContentPageName());
192        contentPageDetailValue.setContentPageLayoutPK(contentPageLayout.getPrimaryKey());
193        contentPageDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
194        contentPageDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
195
196        contentControl.updateContentPageFromValue(contentPageDetailValue, partyPK);
197
198        if(contentPageDescription == null && description != null) {
199            contentControl.createContentPageDescription(contentPage, getPreferredLanguage(), description, partyPK);
200        } else if(contentPageDescription != null && description == null) {
201            contentControl.deleteContentPageDescription(contentPageDescription, partyPK);
202        } else if(contentPageDescription != null && description != null) {
203            ContentPageDescriptionValue contentPageDescriptionValue = contentControl.getContentPageDescriptionValue(contentPageDescription);
204
205            contentPageDescriptionValue.setDescription(description);
206            contentControl.updateContentPageDescriptionFromValue(contentPageDescriptionValue, partyPK);
207        }
208    }
209    
210}