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.GetGeoCodeTimeZonesForm;
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.GeoCodeTimeZone;
028import com.echothree.model.data.geo.server.factory.GeoCodeTimeZoneFactory;
029import com.echothree.model.data.party.server.entity.TimeZone;
030import com.echothree.util.common.command.BaseResult;
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.BasePaginatedMultipleEntitiesCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.Collection;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class GetGeoCodeTimeZonesCommand
045        extends BasePaginatedMultipleEntitiesCommand<GeoCodeTimeZone, GetGeoCodeTimeZonesForm> {
046    
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> FORM_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.GeoCodeTimeZone.name(), SecurityRoles.List.name())
055                ))
056        ));
057        
058        FORM_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("GeoCodeName", FieldType.ENTITY_NAME, false, null, null),
060                new FieldDefinition("JavaTimeZoneName", FieldType.TIME_ZONE_NAME, false, null, null)
061        );
062    }
063
064    @Inject
065    GeoControl geoControl;
066
067    @Inject
068    PartyControl partyControl;
069
070
071    /** Creates a new instance of GetGeoCodeTimeZonesCommand */
072    public GetGeoCodeTimeZonesCommand() {
073        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
074    }
075
076    GeoCode geoCode;
077    TimeZone timeZone;
078
079    @Override
080    protected void handleForm() {
081        var geoCodeName = form.getGeoCodeName();
082        var javaTimeZoneName = form.getJavaTimeZoneName();
083        var parameterCount = (geoCodeName != null ? 1 : 0) + (javaTimeZoneName != null ? 1 : 0);
084
085        if(parameterCount == 1) {
086            if(geoCodeName != null) {
087                geoCode = geoControl.getGeoCodeByName(geoCodeName);
088
089                if(geoCode == null) {
090                    addExecutionError(ExecutionErrors.UnknownGeoCodeName.name(), geoCodeName);
091                }
092            } else {
093                timeZone = partyControl.getTimeZoneByJavaName(javaTimeZoneName);
094
095                if(timeZone == null) {
096                    addExecutionError(ExecutionErrors.UnknownJavaTimeZoneName.name(), javaTimeZoneName);
097                }
098            }
099        } else {
100            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
101        }
102    }
103
104    @Override
105    protected Long getTotalEntities() {
106        Long totalEntities = null;
107
108        if(!hasExecutionErrors()) {
109            if(geoCode != null) {
110                totalEntities = geoControl.countGeoCodeTimeZonesByGeoCode(geoCode);
111            } else {
112                totalEntities = geoControl.countGeoCodeTimeZonesByTimeZone(timeZone);
113            }
114        }
115
116        return totalEntities;
117    }
118
119    @Override
120    protected Collection<GeoCodeTimeZone> getEntities() {
121        Collection<GeoCodeTimeZone> entities = null;
122
123        if(!hasExecutionErrors()) {
124            if(geoCode != null) {
125                entities = geoControl.getGeoCodeTimeZonesByGeoCode(geoCode);
126            } else {
127                entities = geoControl.getGeoCodeTimeZonesByTimeZone(timeZone);
128            }
129        }
130
131        return entities;
132    }
133
134    @Override
135    protected BaseResult getResult(Collection<GeoCodeTimeZone> entities) {
136        var result = GeoResultFactory.getGetGeoCodeTimeZonesResult();
137
138        if(entities != null) {
139            var userVisit = getUserVisit();
140
141            if(geoCode != null) {
142                result.setGeoCode(geoControl.getGeoCodeTransfer(userVisit, geoCode));
143            } else {
144                result.setTimeZone(partyControl.getTimeZoneTransfer(userVisit, timeZone));
145            }
146
147            if(session.hasLimit(GeoCodeTimeZoneFactory.class)) {
148                result.setGeoCodeTimeZoneCount(getTotalEntities());
149            }
150
151            result.setGeoCodeTimeZones(geoControl.getGeoCodeTimeZoneTransfers(userVisit, entities));
152        }
153
154        return result;
155    }
156    
157}