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.model.control.accounting.server.graphql;
018
019import com.echothree.model.control.accounting.server.control.AccountingControl;
020import com.echothree.model.control.geo.server.control.GeoControl;
021import com.echothree.model.control.geo.server.graphql.GeoCodeCurrencyObject;
022import com.echothree.model.control.geo.server.graphql.GeoSecurityUtils;
023import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
024import com.echothree.model.control.graphql.server.graphql.count.Connections;
025import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
026import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
027import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
028import com.echothree.model.control.graphql.server.util.BaseGraphQl;
029import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
030import com.echothree.model.control.user.server.control.UserControl;
031import com.echothree.model.data.accounting.server.entity.Currency;
032import com.echothree.model.data.geo.common.GeoCodeCurrencyConstants;
033import com.echothree.util.server.persistence.Session;
034import graphql.annotations.annotationTypes.GraphQLDescription;
035import graphql.annotations.annotationTypes.GraphQLField;
036import graphql.annotations.annotationTypes.GraphQLName;
037import graphql.annotations.annotationTypes.GraphQLNonNull;
038import graphql.annotations.connection.GraphQLConnection;
039import graphql.schema.DataFetchingEnvironment;
040import java.util.ArrayList;
041import java.util.stream.Collectors;
042
043@GraphQLDescription("currency object")
044@GraphQLName("Currency")
045public class CurrencyObject
046        extends BaseEntityInstanceObject {
047
048    private final Currency currency; // Always Present
049    
050    public CurrencyObject(Currency currency) {
051        super(currency.getPrimaryKey());
052        
053        this.currency = currency;
054    }
055    
056    @GraphQLField
057    @GraphQLDescription("currency iso name")
058    @GraphQLNonNull
059    public String getCurrencyIsoName() {
060        return currency.getCurrencyIsoName();
061    }
062
063    @GraphQLField
064    @GraphQLDescription("symbol")
065    public String getSymbol() {
066        return currency.getSymbol();
067    }
068
069    @GraphQLField
070    @GraphQLDescription("symbol position")
071    public SymbolPositionObject getSymbolPosition(final DataFetchingEnvironment env) {
072        return AccountingSecurityUtils.getHasSymbolPositionAccess(env) ? new SymbolPositionObject(currency.getSymbolPosition()) : null;
073    }
074    
075    @GraphQLField
076    @GraphQLDescription("symbol on list start")
077    public Boolean getSymbolOnListStart() {
078        return currency.getSymbolOnListStart();
079    }
080
081    @GraphQLField
082    @GraphQLDescription("symbol on list member")
083    public Boolean getSymbolOnListMember() {
084        return currency.getSymbolOnListMember();
085    }
086
087    @GraphQLField
088    @GraphQLDescription("symbol on subtotal")
089    public Boolean getSymbolOnSubtotal() {
090        return currency.getSymbolOnSubtotal();
091    }
092
093    @GraphQLField
094    @GraphQLDescription("symbol on total")
095    public Boolean getSymbolOnTotal() {
096        return currency.getSymbolOnTotal();
097    }
098
099    @GraphQLField
100    @GraphQLDescription("grouping separator")
101    @GraphQLNonNull
102    public String getGroupingSeparator() {
103        return currency.getGroupingSeparator();
104    }
105
106    @GraphQLField
107    @GraphQLDescription("grouping size")
108    @GraphQLNonNull
109    public Integer getGroupingSize() {
110        return currency.getGroupingSize();
111    }
112
113    @GraphQLField
114    @GraphQLDescription("fraction separator")
115    public String getFractionSeparator() {
116        return currency.getFractionSeparator();
117    }
118
119    @GraphQLField
120    @GraphQLDescription("default fraction digits")
121    public Integer getDefaultFractionDigits() {
122        return currency.getDefaultFractionDigits();
123    }
124
125    @GraphQLField
126    @GraphQLDescription("price unit fraction digits")
127    public Integer getPriceUnitFractionDigits() {
128        return currency.getPriceUnitFractionDigits();
129    }
130
131    @GraphQLField
132    @GraphQLDescription("price line fraction digits")
133    public Integer getPriceLineFractionDigits() {
134        return currency.getPriceLineFractionDigits();
135    }
136
137    @GraphQLField
138    @GraphQLDescription("cost unit fraction digits")
139    public Integer getCostUnitFractionDigits() {
140        return currency.getCostUnitFractionDigits();
141    }
142
143    @GraphQLField
144    @GraphQLDescription("cost line fraction digits")
145    public Integer getCostLineFractionDigits() {
146        return currency.getCostLineFractionDigits();
147    }
148
149    @GraphQLField
150    @GraphQLDescription("minus sign")
151    @GraphQLNonNull
152    public String getMinusSign() {
153        return currency.getMinusSign();
154    }
155
156    @GraphQLField
157    @GraphQLDescription("is default")
158    @GraphQLNonNull
159    public Boolean getIsDefault() {
160        return currency.getIsDefault();
161    }
162
163    @GraphQLField
164    @GraphQLDescription("sort order")
165    @GraphQLNonNull
166    public Integer getSortOrder() {
167        return currency.getSortOrder();
168    }
169
170    @GraphQLField
171    @GraphQLDescription("description")
172    @GraphQLNonNull
173    public String getDescription(final DataFetchingEnvironment env) {
174        var accountingControl = Session.getModelController(AccountingControl.class);
175        var userControl = Session.getModelController(UserControl.class);
176
177        return accountingControl.getBestCurrencyDescription(currency, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
178    }
179
180    @GraphQLField
181    @GraphQLDescription("geo code currencies")
182    @GraphQLNonNull
183    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
184    public CountingPaginatedData<GeoCodeCurrencyObject> getGeoCodeCurrencies(final DataFetchingEnvironment env) {
185        if(GeoSecurityUtils.getHasGeoCodeCurrenciesAccess(env)) {
186            var geoControl = Session.getModelController(GeoControl.class);
187            var totalCount = geoControl.countGeoCodeCurrenciesByCurrency(currency);
188
189            try(var objectLimiter = new ObjectLimiter(env, GeoCodeCurrencyConstants.COMPONENT_VENDOR_NAME, GeoCodeCurrencyConstants.ENTITY_TYPE_NAME, totalCount)) {
190                var entities = geoControl.getGeoCodeCurrenciesByCurrency(currency);
191                var items = entities.stream().map(GeoCodeCurrencyObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
192
193                return new CountedObjects<>(objectLimiter, items);
194            }
195        } else {
196            return Connections.emptyConnection();
197        }
198    }
199
200}