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.rating.server.command;
018
019import com.echothree.control.user.rating.common.form.CreateRatingForm;
020import com.echothree.control.user.rating.common.result.RatingResultFactory;
021import com.echothree.model.control.core.server.control.EntityInstanceControl;
022import com.echothree.model.control.rating.server.control.RatingControl;
023import com.echothree.model.control.sequence.common.SequenceTypes;
024import com.echothree.model.control.sequence.server.control.SequenceControl;
025import com.echothree.model.control.sequence.server.logic.SequenceGeneratorLogic;
026import com.echothree.model.data.core.server.entity.EntityInstance;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.persistence.BasePK;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.server.control.BaseSimpleCommand;
034import com.echothree.util.server.persistence.Session;
035import java.util.Arrays;
036import java.util.Collections;
037import java.util.List;
038import javax.enterprise.context.RequestScoped;
039
040@RequestScoped
041public class CreateRatingCommand
042        extends BaseSimpleCommand<CreateRatingForm> {
043    
044    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
045    
046    static {
047        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
048            new FieldDefinition("RatedByUsername", FieldType.STRING, false, 1L, 80L),
049            new FieldDefinition("EntityRef", FieldType.ENTITY_REF, true, null, null),
050            new FieldDefinition("RatingTypeName", FieldType.ENTITY_NAME, true, null, null),
051            new FieldDefinition("RatingTypeListItemName", FieldType.ENTITY_NAME, true, null, null)
052        ));
053    }
054    
055    /** Creates a new instance of CreateRatingCommand */
056    public CreateRatingCommand() {
057        super(null, FORM_FIELD_DEFINITIONS, false);
058    }
059    
060    @Override
061    protected BaseResult execute() {
062        var result = RatingResultFactory.getCreateRatingResult();
063        var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
064        String ratingName = null;
065        var entityRef = form.getEntityRef();
066        var ratedEntityInstance = entityInstanceControl.getEntityInstanceByEntityRef(entityRef);
067        
068        if(ratedEntityInstance != null) {
069            var ratingControl = Session.getModelController(RatingControl.class);
070            EntityInstance ratedByEntityInstance = null;
071            BasePK createdBy = getPartyPK();
072            var ratedByUsername = form.getRatedByUsername();
073            
074            if(ratedByUsername != null) {
075                var userControl = getUserControl();
076                var userLogin = userControl.getUserLoginByUsername(ratedByUsername);
077                
078                if(userLogin != null) {
079                    ratedByEntityInstance = entityInstanceControl.getEntityInstanceByBasePK(userLogin.getPartyPK());
080                } else {
081                    addExecutionError(ExecutionErrors.UnknownRatedByUsername.name(), ratedByUsername);
082                }
083            } else {
084                ratedByEntityInstance = entityInstanceControl.getEntityInstanceByBasePK(createdBy);
085            }
086            
087            if(!hasExecutionErrors()) {
088                var rating = ratingControl.getRating(ratedEntityInstance, ratedByEntityInstance);
089                
090                if(rating == null) {
091                    var ratingTypeName = form.getRatingTypeName();
092                    var ratingType = ratingControl.getRatingTypeByName(ratedEntityInstance.getEntityType(),
093                            ratingTypeName);
094                    
095                    if(ratingType != null) {
096                        var ratingTypeListItemName = form.getRatingTypeListItemName();
097                        var ratingTypeListItem = ratingControl.getRatingTypeListItemByName(ratingType, ratingTypeListItemName);
098                        
099                        if(ratingTypeListItem != null) {
100                            var sequenceControl = Session.getModelController(SequenceControl.class);
101                            var ratingSequence = ratingType.getLastDetail().getRatingSequence();
102                            
103                            if(ratingSequence == null) {
104                                ratingSequence = sequenceControl.getDefaultSequenceUsingNames(SequenceTypes.RATING.name());
105                            }
106                            
107                            ratingName = SequenceGeneratorLogic.getInstance().getNextSequenceValue(ratingSequence);
108                            
109                            ratingControl.createRating(ratingName, ratingTypeListItem, ratedEntityInstance, ratedByEntityInstance,
110                                    createdBy);
111                        } else {
112                            addExecutionError(ExecutionErrors.UnknownRatingTypeListItemName.name(), ratingTypeListItemName);
113                        }
114                    } else {
115                        addExecutionError(ExecutionErrors.UnknownRatingTypeName.name(), ratingTypeName);
116                    }
117                } else {
118                    addExecutionError(ExecutionErrors.DuplicateRating.name());
119                }
120            }
121        } else {
122            addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef);
123        }
124        
125        result.setRatingName(ratingName);
126        
127        return result;
128    }
129    
130}