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