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.edit.ContentEditFactory;
020import com.echothree.control.user.content.common.edit.ContentPageLayoutDescriptionEdit;
021import com.echothree.control.user.content.common.result.ContentResultFactory;
022import com.echothree.control.user.content.common.result.EditContentPageLayoutDescriptionResult;
023import com.echothree.control.user.content.common.spec.ContentPageLayoutDescriptionSpec;
024import com.echothree.model.control.content.server.control.ContentControl;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.content.server.entity.ContentPageLayout;
030import com.echothree.model.data.content.server.entity.ContentPageLayoutDescription;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseAbstractEditCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
041
042@Dependent
043public class EditContentPageLayoutDescriptionCommand
044        extends BaseAbstractEditCommand<ContentPageLayoutDescriptionSpec, ContentPageLayoutDescriptionEdit, EditContentPageLayoutDescriptionResult, ContentPageLayoutDescription, ContentPageLayout> {
045    
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
048    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
049    
050    static {
051        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
052                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
054                        new SecurityRoleDefinition(SecurityRoleGroups.ContentPageLayout.name(), SecurityRoles.Description.name())
055                ))
056        ));
057        
058        SPEC_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("ContentPageLayoutName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
061        );
062        
063        EDIT_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
065        );
066    }
067
068    @Inject
069    ContentControl contentControl;
070
071    @Inject
072    PartyControl partyControl;
073
074
075    /** Creates a new instance of EditContentPageLayoutDescriptionCommand */
076    public EditContentPageLayoutDescriptionCommand() {
077        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
078    }
079
080    @Override
081    public EditContentPageLayoutDescriptionResult getResult() {
082        return ContentResultFactory.getEditContentPageLayoutDescriptionResult();
083    }
084
085    @Override
086    public ContentPageLayoutDescriptionEdit getEdit() {
087        return ContentEditFactory.getContentPageLayoutDescriptionEdit();
088    }
089
090    @Override
091    public ContentPageLayoutDescription getEntity(EditContentPageLayoutDescriptionResult result) {
092        ContentPageLayoutDescription contentPageLayoutDescription = null;
093        var contentPageLayoutName = spec.getContentPageLayoutName();
094        var contentPageLayout = contentControl.getContentPageLayoutByName(contentPageLayoutName);
095
096        if(contentPageLayout != null) {
097            var languageIsoName = spec.getLanguageIsoName();
098            var language = partyControl.getLanguageByIsoName(languageIsoName);
099
100            if(language != null) {
101                contentPageLayoutDescription = contentControl.getContentPageLayoutDescription(contentPageLayout, language,
102                        editModeToEntityPermission(editMode));
103
104                if(contentPageLayoutDescription == null) {
105                    addExecutionError(ExecutionErrors.UnknownContentPageLayoutDescription.name(),
106                            contentPageLayout.getLastDetail().getContentPageLayoutName(), language.getLanguageIsoName());
107                }
108            } else {
109                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
110            }
111        } else {
112            addExecutionError(ExecutionErrors.UnknownContentPageLayoutName.name(), contentPageLayoutName);
113        }
114
115        return contentPageLayoutDescription;
116    }
117
118    @Override
119    public ContentPageLayout getLockEntity(ContentPageLayoutDescription contentPageLayoutDescription) {
120        return contentPageLayoutDescription.getContentPageLayout();
121    }
122
123    @Override
124    public void fillInResult(EditContentPageLayoutDescriptionResult result, ContentPageLayoutDescription contentPageLayoutDescription) {
125        result.setContentPageLayoutDescription(contentControl.getContentPageLayoutDescriptionTransfer(getUserVisit(), contentPageLayoutDescription));
126    }
127
128    @Override
129    public void doLock(ContentPageLayoutDescriptionEdit edit, ContentPageLayoutDescription contentPageLayoutDescription) {
130        edit.setDescription(contentPageLayoutDescription.getDescription());
131    }
132
133    @Override
134    public void doUpdate(ContentPageLayoutDescription contentPageLayoutDescription) {
135        var contentPageLayoutDescriptionValue = contentControl.getContentPageLayoutDescriptionValue(contentPageLayoutDescription);
136
137        contentPageLayoutDescriptionValue.setDescription(edit.getDescription());
138
139        contentControl.updateContentPageLayoutDescriptionFromValue(contentPageLayoutDescriptionValue, getPartyPK());
140    }
141    
142}