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.geo.server.command;
018
019import com.echothree.control.user.geo.common.form.GetGeoCodeDateTimeFormatsForm;
020import com.echothree.control.user.geo.common.result.GeoResultFactory;
021import com.echothree.model.control.geo.server.control.GeoControl;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.model.control.security.common.SecurityRoleGroups;
025import com.echothree.model.control.security.common.SecurityRoles;
026import com.echothree.model.data.geo.server.entity.GeoCode;
027import com.echothree.model.data.geo.server.entity.GeoCodeDateTimeFormat;
028import com.echothree.model.data.geo.server.factory.GeoCodeCurrencyFactory;
029import com.echothree.model.data.geo.server.factory.GeoCodeDateTimeFormatFactory;
030import com.echothree.model.data.party.server.entity.DateTimeFormat;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.Collection;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class GetGeoCodeDateTimeFormatsCommand
046        extends BasePaginatedMultipleEntitiesCommand<GeoCodeDateTimeFormat, GetGeoCodeDateTimeFormatsForm> {
047    
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.GeoCodeDateTimeFormat.name(), SecurityRoles.List.name())
056                ))
057        ));
058        
059        FORM_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("GeoCodeName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("DateTimeFormatName", FieldType.ENTITY_NAME, false, null, null)
062        );
063    }
064
065    @Inject
066    GeoControl geoControl;
067
068    @Inject
069    PartyControl partyControl;
070
071
072    /** Creates a new instance of GetGeoCodeDateTimeFormatsCommand */
073    public GetGeoCodeDateTimeFormatsCommand() {
074        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
075    }
076
077    GeoCode geoCode;
078    DateTimeFormat dateTimeFormat;
079
080    @Override
081    protected void handleForm() {
082        var geoCodeName = form.getGeoCodeName();
083        var dateTimeFormatName = form.getDateTimeFormatName();
084        var parameterCount = (geoCodeName != null ? 1 : 0) + (dateTimeFormatName != null ? 1 : 0);
085
086        if(parameterCount == 1) {
087            if(geoCodeName != null) {
088                geoCode = geoControl.getGeoCodeByName(geoCodeName);
089
090                if(geoCode == null) {
091                    addExecutionError(ExecutionErrors.UnknownGeoCodeName.name(), geoCodeName);
092                }
093            } else {
094                dateTimeFormat = partyControl.getDateTimeFormatByName(dateTimeFormatName);
095
096                if(dateTimeFormat == null) {
097                    addExecutionError(ExecutionErrors.UnknownDateTimeFormatName.name(), dateTimeFormatName);
098                }
099            }
100        } else {
101            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
102        }
103    }
104
105    @Override
106    protected Long getTotalEntities() {
107        Long totalEntities = null;
108
109        if(!hasExecutionErrors()) {
110            if(geoCode != null) {
111                totalEntities = geoControl.countGeoCodeDateTimeFormatsByGeoCode(geoCode);
112            } else {
113                totalEntities = geoControl.countGeoCodeDateTimeFormatsByDateTimeFormat(dateTimeFormat);
114            }
115        }
116
117        return totalEntities;
118    }
119
120    @Override
121    protected Collection<GeoCodeDateTimeFormat> getEntities() {
122        Collection<GeoCodeDateTimeFormat> entities = null;
123
124        if(!hasExecutionErrors()) {
125            if(geoCode != null) {
126                entities = geoControl.getGeoCodeDateTimeFormatsByGeoCode(geoCode);
127            } else {
128                entities = geoControl.getGeoCodeDateTimeFormatsByDateTimeFormat(dateTimeFormat);
129            }
130        }
131
132        return entities;
133    }
134
135    @Override
136    protected BaseResult getResult(Collection<GeoCodeDateTimeFormat> entities) {
137        var result = GeoResultFactory.getGetGeoCodeDateTimeFormatsResult();
138
139        if(entities != null) {
140            var userVisit = getUserVisit();
141
142            if(geoCode != null) {
143                result.setGeoCode(geoControl.getGeoCodeTransfer(userVisit, geoCode));
144            } else {
145                result.setDateTimeFormat(partyControl.getDateTimeFormatTransfer(userVisit, dateTimeFormat));
146            }
147
148            if(session.hasLimit(GeoCodeDateTimeFormatFactory.class)) {
149                result.setGeoCodeDateTimeFormatCount(getTotalEntities());
150            }
151
152            result.setGeoCodeDateTimeFormats(geoControl.getGeoCodeDateTimeFormatTransfers(userVisit, entities));
153        }
154
155        return result;
156    }
157    
158}