001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.model.control.party.server.graphql;
018
019import com.echothree.model.control.accounting.server.graphql.AccountingSecurityUtils;
020import com.echothree.model.control.graphql.server.util.BaseGraphQl;
021import com.echothree.model.control.accounting.server.graphql.CurrencyObject;
022import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
023import com.echothree.model.control.graphql.server.graphql.count.Connections;
024import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
025import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
026import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
027import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
028import com.echothree.model.control.party.server.control.PartyControl;
029import com.echothree.model.control.user.server.control.UserControl;
030import com.echothree.model.data.accounting.server.entity.Currency;
031import com.echothree.model.data.party.common.PartyAliasConstants;
032import com.echothree.model.data.party.server.entity.DateTimeFormat;
033import com.echothree.model.data.party.server.entity.Language;
034import com.echothree.model.data.party.server.entity.Party;
035import com.echothree.model.data.party.server.entity.PartyDetail;
036import com.echothree.model.data.party.server.entity.PartyGroup;
037import com.echothree.model.data.party.server.entity.Person;
038import com.echothree.model.data.party.server.entity.TimeZone;
039import com.echothree.util.server.persistence.Session;
040import graphql.annotations.annotationTypes.GraphQLDescription;
041import graphql.annotations.annotationTypes.GraphQLField;
042import graphql.annotations.annotationTypes.GraphQLNonNull;
043import graphql.annotations.connection.GraphQLConnection;
044import graphql.schema.DataFetchingEnvironment;
045import java.util.ArrayList;
046import java.util.stream.Collectors;
047
048public abstract class BasePartyObject
049        extends BaseEntityInstanceObject {
050
051    protected final Party party; // Always Present
052
053    protected BasePartyObject(Party party) {
054        super(party.getPrimaryKey());
055        
056        this.party = party;
057    }
058
059    private PartyDetail partyDetail; // Optional, use getPartyDetail()
060
061    protected PartyDetail getPartyDetail() {
062        if(partyDetail == null) {
063            partyDetail = party.getLastDetail();
064        }
065        
066        return partyDetail;
067    }
068    
069    @GraphQLField
070    @GraphQLDescription("party name")
071    @GraphQLNonNull
072    public String getPartyName() {
073        return getPartyDetail().getPartyName();
074    }
075
076    @GraphQLField
077    @GraphQLDescription("party type")
078    @GraphQLNonNull
079    public PartyTypeObject getPartyType(final DataFetchingEnvironment env) {
080        return PartySecurityUtils.getHasPartyTypeAccess(env) ? new PartyTypeObject(getPartyDetail().getPartyType()) : null;
081    }
082
083    @GraphQLField
084    @GraphQLDescription("preferred language")
085    public LanguageObject getPreferredLanguage(final DataFetchingEnvironment env) {
086        Language preferredLanguage = getPartyDetail().getPreferredLanguage();
087        
088        return preferredLanguage != null && PartySecurityUtils.getHasLanguageAccess(env) ? new LanguageObject(preferredLanguage) : null;
089    }
090
091    @GraphQLField
092    @GraphQLDescription("preferred language")
093    public CurrencyObject getPreferredCurrency(final DataFetchingEnvironment env) {
094        Currency preferredCurrency = getPartyDetail().getPreferredCurrency();
095        
096        return preferredCurrency != null && AccountingSecurityUtils.getHasCurrencyAccess(env) ? new CurrencyObject(preferredCurrency) : null;
097    }
098
099    @GraphQLField
100    @GraphQLDescription("preferred time zone")
101    public TimeZoneObject getPreferredTimeZone(final DataFetchingEnvironment env) {
102        TimeZone preferredTimeZone = getPartyDetail().getPreferredTimeZone();
103        
104        return preferredTimeZone != null && PartySecurityUtils.getHasTimeZoneAccess(env) ? new TimeZoneObject(preferredTimeZone) : null;
105    }
106
107    @GraphQLField
108    @GraphQLDescription("preferred date time format")
109    public DateTimeFormatObject getPreferredDateTimeFormat(final DataFetchingEnvironment env) {
110        DateTimeFormat preferredDateTimeFormat = getPartyDetail().getPreferredDateTimeFormat();
111        
112        return preferredDateTimeFormat != null && PartySecurityUtils.getHasDateTimeFormatAccess(env) ? new DateTimeFormatObject(preferredDateTimeFormat) : null;
113    }
114    
115    @GraphQLField
116    @GraphQLDescription("person")
117    public PersonObject getPerson(final DataFetchingEnvironment env) {
118        var partyControl = Session.getModelController(PartyControl.class);
119        Person person = partyControl.getPerson(party);
120        
121        return person == null ? null : new PersonObject(person);
122    }
123    
124    @GraphQLField
125    @GraphQLDescription("party group")
126    public PartyGroupObject getPartyGroup(final DataFetchingEnvironment env) {
127        var partyControl = Session.getModelController(PartyControl.class);
128        PartyGroup partyGroup = partyControl.getPartyGroup(party);
129        
130        return partyGroup == null ? null : new PartyGroupObject(partyGroup);
131    }
132    
133    @GraphQLField
134    @GraphQLDescription("description")
135    @GraphQLNonNull
136    public String getDescription(final DataFetchingEnvironment env) {
137        var partyControl = Session.getModelController(PartyControl.class);
138        var userControl = Session.getModelController(UserControl.class);
139
140        return partyControl.getBestPartyDescription(party, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
141    }
142
143    @GraphQLField
144    @GraphQLDescription("party aliases")
145    @GraphQLNonNull
146    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
147    public CountingPaginatedData<PartyAliasObject> getPartyAliases(final DataFetchingEnvironment env) {
148        if(PartySecurityUtils.getHasPartyAliasesAccess(env, party)) {
149            var partyControl = Session.getModelController(PartyControl.class);
150            var totalCount = partyControl.countPartyAliasesByParty(party);
151
152            try(var objectLimiter = new ObjectLimiter(env, PartyAliasConstants.COMPONENT_VENDOR_NAME, PartyAliasConstants.ENTITY_TYPE_NAME, totalCount)) {
153                var entities = partyControl.getPartyAliasesByParty(party);
154                var partyAliases = entities.stream().map(PartyAliasObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
155
156                return new CountedObjects<>(objectLimiter, partyAliases);
157            }
158        } else {
159            return Connections.emptyConnection();
160        }
161    }
162
163}