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// Generated File -- DO NOT EDIT BY HAND
017// --------------------------------------------------------------------------------
018
019/**
020 * GlAccountDetailFactory.java
021 */
022
023package com.echothree.model.data.accounting.server.factory;
024
025import com.echothree.model.data.accounting.common.pk.GlAccountPK;
026import com.echothree.model.data.accounting.common.pk.GlAccountTypePK;
027import com.echothree.model.data.accounting.common.pk.GlAccountClassPK;
028import com.echothree.model.data.accounting.common.pk.GlAccountCategoryPK;
029import com.echothree.model.data.accounting.common.pk.GlResourceTypePK;
030import com.echothree.model.data.accounting.common.pk.CurrencyPK;
031
032import com.echothree.model.data.accounting.server.entity.GlAccount;
033import com.echothree.model.data.accounting.server.entity.GlAccountType;
034import com.echothree.model.data.accounting.server.entity.GlAccountClass;
035import com.echothree.model.data.accounting.server.entity.GlAccountCategory;
036import com.echothree.model.data.accounting.server.entity.GlResourceType;
037import com.echothree.model.data.accounting.server.entity.Currency;
038
039import com.echothree.model.data.accounting.common.GlAccountDetailConstants;
040import com.echothree.model.data.accounting.common.pk.GlAccountDetailPK;
041import com.echothree.model.data.accounting.server.value.GlAccountDetailValue;
042import com.echothree.model.data.accounting.server.entity.GlAccountDetail;
043import com.echothree.util.common.exception.PersistenceDatabaseException;
044import com.echothree.util.common.exception.PersistenceDatabaseUpdateException;
045import com.echothree.util.common.exception.PersistenceNotNullException;
046import com.echothree.util.server.persistence.BaseFactory;
047import com.echothree.util.server.persistence.EntityIdGenerator;
048import com.echothree.util.server.persistence.EntityPermission;
049import com.echothree.util.server.persistence.PersistenceDebugFlags;
050import com.echothree.util.server.persistence.Session;
051import java.sql.PreparedStatement;
052import java.sql.ResultSet;
053import java.sql.SQLException;
054import java.sql.Types;
055import java.io.ByteArrayInputStream;
056import java.io.StringReader;
057import java.util.ArrayList;
058import java.util.Collection;
059import java.util.HashSet;
060import java.util.List;
061import java.util.Map;
062import java.util.Set;
063import javax.enterprise.context.ApplicationScoped;
064import javax.enterprise.inject.spi.CDI;
065import javax.inject.Inject;
066import org.jooq.ResultQuery;
067import org.slf4j.Logger;
068import org.slf4j.LoggerFactory;
069
070@ApplicationScoped
071public class GlAccountDetailFactory
072        implements BaseFactory<GlAccountDetailPK, GlAccountDetail> {
073    
074    @Inject
075    Session session;
076    
077    //private static final Logger log = LoggerFactory.getLogger(GlAccountDetailFactory.class);
078    
079    final private static String SQL_SELECT_READ_ONLY = "SELECT gladt_glaccountdetailid, gladt_gla_glaccountid, gladt_glaccountname, gladt_parentglaccountid, gladt_glatyp_glaccounttypeid, gladt_glacls_glaccountclassid, gladt_glac_glaccountcategoryid, gladt_glrtyp_glresourcetypeid, gladt_cur_currencyid, gladt_isdefault, gladt_fromtime, gladt_thrutime FROM glaccountdetails WHERE gladt_glaccountdetailid = ?";
080    final private static String SQL_SELECT_READ_WRITE = "SELECT gladt_glaccountdetailid, gladt_gla_glaccountid, gladt_glaccountname, gladt_parentglaccountid, gladt_glatyp_glaccounttypeid, gladt_glacls_glaccountclassid, gladt_glac_glaccountcategoryid, gladt_glrtyp_glresourcetypeid, gladt_cur_currencyid, gladt_isdefault, gladt_fromtime, gladt_thrutime FROM glaccountdetails WHERE gladt_glaccountdetailid = ? FOR UPDATE";
081    final private static String SQL_INSERT = "INSERT INTO glaccountdetails (gladt_glaccountdetailid, gladt_gla_glaccountid, gladt_glaccountname, gladt_parentglaccountid, gladt_glatyp_glaccounttypeid, gladt_glacls_glaccountclassid, gladt_glac_glaccountcategoryid, gladt_glrtyp_glresourcetypeid, gladt_cur_currencyid, gladt_isdefault, gladt_fromtime, gladt_thrutime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
082    final private static String SQL_UPDATE = "UPDATE glaccountdetails SET gladt_gla_glaccountid = ?, gladt_glaccountname = ?, gladt_parentglaccountid = ?, gladt_glatyp_glaccounttypeid = ?, gladt_glacls_glaccountclassid = ?, gladt_glac_glaccountcategoryid = ?, gladt_glrtyp_glresourcetypeid = ?, gladt_cur_currencyid = ?, gladt_isdefault = ?, gladt_fromtime = ?, gladt_thrutime = ? WHERE gladt_glaccountdetailid = ?";
083    final private static String SQL_DELETE = "DELETE FROM glaccountdetails WHERE gladt_glaccountdetailid = ?";
084    final private static String SQL_VALID = "SELECT COUNT(*) FROM glaccountdetails WHERE gladt_glaccountdetailid = ?";
085    
086    final public static String GLADT_GLACCOUNTDETAILID = "gladt_glaccountdetailid";
087    final public static String GLADT_GLA_GLACCOUNTID = "gladt_gla_glaccountid";
088    final public static String GLADT_GLACCOUNTNAME = "gladt_glaccountname";
089    final public static String GLADT_PARENTGLACCOUNTID = "gladt_parentglaccountid";
090    final public static String GLADT_GLATYP_GLACCOUNTTYPEID = "gladt_glatyp_glaccounttypeid";
091    final public static String GLADT_GLACLS_GLACCOUNTCLASSID = "gladt_glacls_glaccountclassid";
092    final public static String GLADT_GLAC_GLACCOUNTCATEGORYID = "gladt_glac_glaccountcategoryid";
093    final public static String GLADT_GLRTYP_GLRESOURCETYPEID = "gladt_glrtyp_glresourcetypeid";
094    final public static String GLADT_CUR_CURRENCYID = "gladt_cur_currencyid";
095    final public static String GLADT_ISDEFAULT = "gladt_isdefault";
096    final public static String GLADT_FROMTIME = "gladt_fromtime";
097    final public static String GLADT_THRUTIME = "gladt_thrutime";
098    
099    final private static EntityIdGenerator entityIdGenerator = new EntityIdGenerator(GlAccountDetailConstants.COMPONENT_VENDOR_NAME, GlAccountDetailConstants.ENTITY_TYPE_NAME);
100    
101    /** Creates a new instance of GlAccountDetailFactory */
102    protected GlAccountDetailFactory() {
103        super();
104    }
105    
106    public static GlAccountDetailFactory getInstance() {
107        return CDI.current().select(GlAccountDetailFactory.class).get();
108    }
109    
110    @Override
111    public String getComponentVendorName() {
112        return GlAccountDetailConstants.COMPONENT_VENDOR_NAME;
113    }
114    
115    @Override
116    public String getEntityTypeName() {
117        return GlAccountDetailConstants.ENTITY_TYPE_NAME;
118    }
119    
120    public PreparedStatement prepareStatement(String query) {
121        return session.prepareStatement(query);
122    }
123    
124    public GlAccountDetailPK getNextPK() {
125        return new GlAccountDetailPK(entityIdGenerator.getNextEntityId());
126    }
127    
128    public Set<GlAccountDetailPK> getPKsFromResultSetAsSet(ResultSet rs)
129            throws PersistenceDatabaseException {
130        Set<GlAccountDetailPK> _result = new HashSet<>();
131        
132        try {
133            while(rs.next()) {
134                _result.add(getPKFromResultSet(rs));
135            }
136        } catch (SQLException se) {
137            throw new PersistenceDatabaseException(se);
138        }
139        
140        return _result;
141    }
142    
143    public java.util.List<GlAccountDetailPK> getPKsFromResultSetAsList(ResultSet rs)
144            throws PersistenceDatabaseException {
145        java.util.List<GlAccountDetailPK> _result = new ArrayList<>();
146        
147        try {
148            while(rs.next()) {
149                _result.add(getPKFromResultSet(rs));
150            }
151        } catch (SQLException se) {
152            throw new PersistenceDatabaseException(se);
153        }
154        
155        return _result;
156    }
157    
158    public GlAccountDetailPK getPKFromResultSet(ResultSet rs)
159            throws PersistenceDatabaseException {
160        GlAccountDetailPK _result;
161        
162        try {
163            long gladt_glaccountdetailid = rs.getLong(GLADT_GLACCOUNTDETAILID);
164            Long _entityId = rs.wasNull() ? null : gladt_glaccountdetailid;
165            
166            _result = new GlAccountDetailPK(_entityId);
167        } catch (SQLException se) {
168            throw new PersistenceDatabaseException(se);
169        }
170        
171        return _result;
172    }
173    
174    public java.util.List<GlAccountDetailValue> getValuesFromPKs(Collection<GlAccountDetailPK> pks)
175            throws PersistenceDatabaseException {
176        java.util.List<GlAccountDetailValue> _values = new ArrayList<>(pks.size());
177        
178        for(GlAccountDetailPK _pk: pks) {
179            _values.add(getValueFromPK(_pk));
180        }
181        
182        return _values;
183    }
184    
185    public GlAccountDetailValue getValueFromPK(GlAccountDetailPK pk)
186            throws PersistenceDatabaseException {
187        GlAccountDetailValue _value;
188        
189        // See if we already have the entity in the session cache
190        GlAccountDetail _entity = (GlAccountDetail)session.getEntity(pk);
191        if(_entity == null)
192            _value = getEntityFromPK(EntityPermission.READ_ONLY, pk).getGlAccountDetailValue();
193        else
194            _value = _entity.getGlAccountDetailValue();
195        
196        return _value;
197    }
198    
199    public java.util.List<GlAccountDetailValue> getValuesFromResultSet(ResultSet rs)
200            throws PersistenceDatabaseException {
201        java.util.List<GlAccountDetailValue> _result = new ArrayList<>();
202        
203        try {
204            while(rs.next()) {
205                _result.add(getValueFromResultSet(rs));
206            }
207        } catch (SQLException se) {
208            throw new PersistenceDatabaseException(se);
209        }
210        
211        return _result;
212    }
213    
214    public GlAccountDetailValue getValueFromResultSet(ResultSet rs)
215            throws PersistenceDatabaseException {
216        GlAccountDetailValue _value;
217        
218        try {
219            Long gladt_glaccountdetailid = rs.getLong(GLADT_GLACCOUNTDETAILID);
220            GlAccountDetailPK _pk = new GlAccountDetailPK(gladt_glaccountdetailid);
221            
222            // See if we already have the entity in the session cache
223            GlAccountDetail _entity = (GlAccountDetail)session.getEntity(_pk);
224            
225            if(_entity == null) {
226                Long gladt_gla_glaccountid = rs.getLong(GLADT_GLA_GLACCOUNTID);
227                if(rs.wasNull())
228                    gladt_gla_glaccountid = null;
229                
230                String gladt_glaccountname = rs.getString(GLADT_GLACCOUNTNAME);
231                if(rs.wasNull())
232                    gladt_glaccountname = null;
233                
234                Long gladt_parentglaccountid = rs.getLong(GLADT_PARENTGLACCOUNTID);
235                if(rs.wasNull())
236                    gladt_parentglaccountid = null;
237                
238                Long gladt_glatyp_glaccounttypeid = rs.getLong(GLADT_GLATYP_GLACCOUNTTYPEID);
239                if(rs.wasNull())
240                    gladt_glatyp_glaccounttypeid = null;
241                
242                Long gladt_glacls_glaccountclassid = rs.getLong(GLADT_GLACLS_GLACCOUNTCLASSID);
243                if(rs.wasNull())
244                    gladt_glacls_glaccountclassid = null;
245                
246                Long gladt_glac_glaccountcategoryid = rs.getLong(GLADT_GLAC_GLACCOUNTCATEGORYID);
247                if(rs.wasNull())
248                    gladt_glac_glaccountcategoryid = null;
249                
250                Long gladt_glrtyp_glresourcetypeid = rs.getLong(GLADT_GLRTYP_GLRESOURCETYPEID);
251                if(rs.wasNull())
252                    gladt_glrtyp_glresourcetypeid = null;
253                
254                Long gladt_cur_currencyid = rs.getLong(GLADT_CUR_CURRENCYID);
255                if(rs.wasNull())
256                    gladt_cur_currencyid = null;
257                
258                Boolean gladt_isdefault = rs.getInt(GLADT_ISDEFAULT) == 1;
259                if(rs.wasNull())
260                    gladt_isdefault = null;
261                
262                Long gladt_fromtime = rs.getLong(GLADT_FROMTIME);
263                if(rs.wasNull())
264                    gladt_fromtime = null;
265                
266                Long gladt_thrutime = rs.getLong(GLADT_THRUTIME);
267                if(rs.wasNull())
268                    gladt_thrutime = null;
269                
270                _value = new GlAccountDetailValue(_pk, new GlAccountPK(gladt_gla_glaccountid), gladt_glaccountname, new GlAccountPK(gladt_parentglaccountid), new GlAccountTypePK(gladt_glatyp_glaccounttypeid), new GlAccountClassPK(gladt_glacls_glaccountclassid), new GlAccountCategoryPK(gladt_glac_glaccountcategoryid), new GlResourceTypePK(gladt_glrtyp_glresourcetypeid), new CurrencyPK(gladt_cur_currencyid), gladt_isdefault, gladt_fromtime, gladt_thrutime);
271            } else
272                _value = _entity.getGlAccountDetailValue();
273        } catch (SQLException se) {
274            throw new PersistenceDatabaseException(se);
275        }
276        
277        return _value;
278    }
279    
280    public java.util.List<GlAccountDetail> getEntitiesFromPKs(EntityPermission entityPermission, Collection<GlAccountDetailPK> pks)
281            throws PersistenceDatabaseException {
282        java.util.List<GlAccountDetail> _entities = new ArrayList<>(pks.size());
283        
284        for(GlAccountDetailPK _pk: pks) {
285            _entities.add(getEntityFromPK(entityPermission, _pk));
286        }
287        
288        return _entities;
289    }
290    
291    public GlAccountDetail getEntityFromValue(EntityPermission entityPermission, GlAccountDetailValue value) {
292        return getEntityFromPK(entityPermission, value.getPrimaryKey());
293    }
294    
295    public GlAccountDetail getEntityFromCache(GlAccountDetailPK pk) {
296        GlAccountDetailValue _value = (GlAccountDetailValue)session.getValueCache().get(pk);
297    
298        return _value == null ? null : new GlAccountDetail(_value, EntityPermission.READ_ONLY);
299    }
300    
301    public GlAccountDetail getEntityFromPK(EntityPermission entityPermission, GlAccountDetailPK pk)
302            throws PersistenceDatabaseException {
303        GlAccountDetail _entity;
304        
305        // See if we already have the entity in the session cache
306        _entity = (GlAccountDetail)session.getEntity(pk);
307        if(_entity != null) {
308            // If the requested permission is READ_WRITE, and the cached permission is
309            // READ_ONLY, then pretend that the cached object wasn't found, and create
310            // a new entity that is READ_WRITE.
311            if(entityPermission.equals(EntityPermission.READ_WRITE)) {
312                if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
313                    _entity = null;
314            }
315        }
316        
317        if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
318            _entity = getEntityFromCache(pk);
319        }
320        
321        if(_entity == null) {
322            PreparedStatement _ps = session.prepareStatement(entityPermission.equals(EntityPermission.READ_ONLY)? SQL_SELECT_READ_ONLY: SQL_SELECT_READ_WRITE);
323            long _entityId = pk.getEntityId();
324            ResultSet _rs = null;
325            
326            try {
327                _ps.setLong(1, _entityId);
328                _rs = _ps.executeQuery();
329                if(_rs.next()) {
330                    _entity = getEntityFromResultSet(entityPermission, _rs);
331                }
332            } catch (SQLException se) {
333                throw new PersistenceDatabaseException(se);
334            } finally {
335                if(_rs != null) {
336                    try {
337                        _rs.close();
338                    } catch (SQLException se) {
339                        // do nothing
340                    }
341                }
342            }
343        }
344        
345        return _entity;
346    }
347    
348    public Set<GlAccountDetailPK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params)
349            throws PersistenceDatabaseException {
350        Set<GlAccountDetailPK> _pks;
351        ResultSet _rs = null;
352        
353        try {
354            if(params.length != 0) {
355                Session.setQueryParams(ps, params);
356            }
357            
358            _rs = ps.executeQuery();
359            _pks = getPKsFromResultSetAsSet(_rs);
360            _rs.close();
361        } catch (SQLException se) {
362            throw new PersistenceDatabaseException(se);
363        } finally {
364            if(_rs != null) {
365                try {
366                    _rs.close();
367                } catch (SQLException se) {
368                    // do nothing
369                }
370            }
371        }
372        
373        return _pks;
374    }
375    
376    public java.util.List<GlAccountDetailPK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params)
377            throws PersistenceDatabaseException {
378        java.util.List<GlAccountDetailPK> _pks;
379        ResultSet _rs = null;
380        
381        try {
382            if(params.length != 0) {
383                Session.setQueryParams(ps, params);
384            }
385            
386            _rs = ps.executeQuery();
387            _pks = getPKsFromResultSetAsList(_rs);
388            _rs.close();
389        } catch (SQLException se) {
390            throw new PersistenceDatabaseException(se);
391        } finally {
392            if(_rs != null) {
393                try {
394                    _rs.close();
395                } catch (SQLException se) {
396                    // do nothing
397                }
398            }
399        }
400        
401        return _pks;
402    }
403    
404    public GlAccountDetailPK getPKFromQuery(PreparedStatement ps, final Object... params)
405            throws PersistenceDatabaseException {
406        GlAccountDetailPK _pk = null;
407        ResultSet _rs = null;
408        
409        try {
410            if(params.length != 0) {
411                Session.setQueryParams(ps, params);
412            }
413            
414            _rs = ps.executeQuery();
415            if(_rs.next()) {
416                _pk = getPKFromResultSet(_rs);
417            }
418            _rs.close();
419        } catch (SQLException se) {
420            throw new PersistenceDatabaseException(se);
421        } finally {
422            if(_rs != null) {
423                try {
424                    _rs.close();
425                } catch (SQLException se) {
426                    // do nothing
427                }
428            }
429        }
430        
431        return _pk;
432    }
433    
434    public java.util.List<GlAccountDetail> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
435            throws PersistenceDatabaseException {
436        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
437        
438        return getEntitiesFromQuery(entityPermission, ps, params);
439    }
440    
441    public java.util.List<GlAccountDetail> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
442            throws PersistenceDatabaseException {
443        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
444        
445        return getEntitiesFromQuery(entityPermission, ps);
446    }
447    
448    public java.util.List<GlAccountDetail> getEntitiesFromQuery(EntityPermission entityPermission, ResultQuery<?> query)
449            throws PersistenceDatabaseException {
450        PreparedStatement ps = session.prepareStatement(query.getSQL());
451        
452        return getEntitiesFromQuery(entityPermission, ps, query.getBindValues().toArray());
453    }
454    
455    public java.util.List<GlAccountDetail> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
456            throws PersistenceDatabaseException {
457        java.util.List<GlAccountDetail> _entities;
458        ResultSet _rs = null;
459        
460        try {
461            if(params.length != 0) {
462                Session.setQueryParams(ps, params);
463            }
464            
465            _rs = ps.executeQuery();
466            _entities = getEntitiesFromResultSet(entityPermission, _rs);
467            _rs.close();
468        } catch (SQLException se) {
469            throw new PersistenceDatabaseException(se);
470        } finally {
471            if(_rs != null) {
472                try {
473                    _rs.close();
474                } catch (SQLException se) {
475                    // do nothing
476                }
477            }
478        }
479        
480        return _entities;
481    }
482    
483    public GlAccountDetail getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
484            throws PersistenceDatabaseException {
485        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
486        
487        return getEntityFromQuery(entityPermission, ps, params);
488    }
489    
490    public GlAccountDetail getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
491            throws PersistenceDatabaseException {
492        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
493        
494        return getEntityFromQuery(entityPermission, ps);
495    }
496    
497    public GlAccountDetail getEntityFromQuery(EntityPermission entityPermission, ResultQuery<?> query)
498            throws PersistenceDatabaseException {
499        PreparedStatement ps = session.prepareStatement(query.getSQL());
500        
501        return getEntityFromQuery(entityPermission, ps, query.getBindValues().toArray());
502    }
503    
504    public GlAccountDetail getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
505            throws PersistenceDatabaseException {
506        GlAccountDetail _entity = null;
507        ResultSet _rs = null;
508        
509        try {
510            if(params.length != 0) {
511                Session.setQueryParams(ps, params);
512            }
513            
514            _rs = ps.executeQuery();
515            if(_rs.next()) {
516                _entity = getEntityFromResultSet(entityPermission, _rs);
517            }
518            _rs.close();
519        } catch (SQLException se) {
520            throw new PersistenceDatabaseException(se);
521        } finally {
522            if(_rs != null) {
523                try {
524                    _rs.close();
525                } catch (SQLException se) {
526                    // do nothing
527                }
528            }
529        }
530        
531        return _entity;
532    }
533    
534    public java.util.List<GlAccountDetail> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs)
535            throws PersistenceDatabaseException {
536        java.util.List<GlAccountDetail> _result = new ArrayList<>();
537        
538        try {
539            while(rs.next()) {
540                _result.add(getEntityFromResultSet(entityPermission, rs));
541            }
542        } catch (SQLException se) {
543            throw new PersistenceDatabaseException(se);
544        }
545        
546        return _result;
547    }
548    
549    public GlAccountDetail getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs)
550            throws PersistenceDatabaseException {
551        GlAccountDetail _entity;
552        
553        try {
554            Long gladt_glaccountdetailid = rs.getLong(GLADT_GLACCOUNTDETAILID);
555            GlAccountDetailPK _pk = new GlAccountDetailPK(gladt_glaccountdetailid);
556            
557            // See if we already have the entity in the session cache
558            _entity = (GlAccountDetail)session.getEntity(_pk);
559            if(_entity != null) {
560                // If the requested permission is READ_WRITE, and the cached permission is
561                // READ_ONLY, then pretend that the cached object wasn't found, and create
562                // a new entity that is READ_WRITE.
563                if(entityPermission.equals(EntityPermission.READ_WRITE)) {
564                    if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
565                        _entity = null;
566                }
567            }
568            boolean foundInSessionCache = _entity != null;
569            
570            if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
571                _entity = getEntityFromCache(_pk);
572            }
573            
574            if(_entity == null) {
575                Long gladt_gla_glaccountid = rs.getLong(GLADT_GLA_GLACCOUNTID);
576                if(rs.wasNull())
577                    gladt_gla_glaccountid = null;
578                
579                String gladt_glaccountname = rs.getString(GLADT_GLACCOUNTNAME);
580                if(rs.wasNull())
581                    gladt_glaccountname = null;
582                
583                Long gladt_parentglaccountid = rs.getLong(GLADT_PARENTGLACCOUNTID);
584                if(rs.wasNull())
585                    gladt_parentglaccountid = null;
586                
587                Long gladt_glatyp_glaccounttypeid = rs.getLong(GLADT_GLATYP_GLACCOUNTTYPEID);
588                if(rs.wasNull())
589                    gladt_glatyp_glaccounttypeid = null;
590                
591                Long gladt_glacls_glaccountclassid = rs.getLong(GLADT_GLACLS_GLACCOUNTCLASSID);
592                if(rs.wasNull())
593                    gladt_glacls_glaccountclassid = null;
594                
595                Long gladt_glac_glaccountcategoryid = rs.getLong(GLADT_GLAC_GLACCOUNTCATEGORYID);
596                if(rs.wasNull())
597                    gladt_glac_glaccountcategoryid = null;
598                
599                Long gladt_glrtyp_glresourcetypeid = rs.getLong(GLADT_GLRTYP_GLRESOURCETYPEID);
600                if(rs.wasNull())
601                    gladt_glrtyp_glresourcetypeid = null;
602                
603                Long gladt_cur_currencyid = rs.getLong(GLADT_CUR_CURRENCYID);
604                if(rs.wasNull())
605                    gladt_cur_currencyid = null;
606                
607                Boolean gladt_isdefault = rs.getInt(GLADT_ISDEFAULT) == 1;
608                if(rs.wasNull())
609                    gladt_isdefault = null;
610                
611                Long gladt_fromtime = rs.getLong(GLADT_FROMTIME);
612                if(rs.wasNull())
613                    gladt_fromtime = null;
614                
615                Long gladt_thrutime = rs.getLong(GLADT_THRUTIME);
616                if(rs.wasNull())
617                    gladt_thrutime = null;
618                
619                GlAccountDetailValue _value = new GlAccountDetailValue(_pk, gladt_gla_glaccountid == null? null: new GlAccountPK(gladt_gla_glaccountid), gladt_glaccountname, gladt_parentglaccountid == null? null: new GlAccountPK(gladt_parentglaccountid), gladt_glatyp_glaccounttypeid == null? null: new GlAccountTypePK(gladt_glatyp_glaccounttypeid), gladt_glacls_glaccountclassid == null? null: new GlAccountClassPK(gladt_glacls_glaccountclassid), gladt_glac_glaccountcategoryid == null? null: new GlAccountCategoryPK(gladt_glac_glaccountcategoryid), gladt_glrtyp_glresourcetypeid == null? null: new GlResourceTypePK(gladt_glrtyp_glresourcetypeid), gladt_cur_currencyid == null? null: new CurrencyPK(gladt_cur_currencyid), gladt_isdefault, gladt_fromtime, gladt_thrutime);
620                _entity = new GlAccountDetail(_value, entityPermission);
621            }
622            
623            if(!foundInSessionCache) {
624                if(entityPermission.equals(EntityPermission.READ_ONLY)) {
625                    session.putReadOnlyEntity(_pk, _entity);
626                    session.getValueCache().put(_entity.getGlAccountDetailValue());
627                } else {
628                    session.putReadWriteEntity(_pk, _entity);
629                }
630            }
631        } catch (SQLException se) {
632            throw new PersistenceDatabaseException(se);
633        }
634        
635        return _entity;
636    }
637    
638    public GlAccountDetail create(GlAccount glAccount, String glAccountName, GlAccount parentGlAccount, GlAccountType glAccountType, GlAccountClass glAccountClass, GlAccountCategory glAccountCategory, GlResourceType glResourceType, Currency currency, Boolean isDefault, Long fromTime, Long thruTime)
639            throws PersistenceDatabaseException, PersistenceNotNullException {
640        return create(glAccount == null ? null : glAccount.getPrimaryKey(), glAccountName, parentGlAccount == null ? null : parentGlAccount.getPrimaryKey(), glAccountType == null ? null : glAccountType.getPrimaryKey(), glAccountClass == null ? null : glAccountClass.getPrimaryKey(), glAccountCategory == null ? null : glAccountCategory.getPrimaryKey(), glResourceType == null ? null : glResourceType.getPrimaryKey(), currency == null ? null : currency.getPrimaryKey(), isDefault, fromTime, thruTime);
641    }
642    
643    private void bindForCreate(PreparedStatement _ps, GlAccountDetailValue _value)
644            throws SQLException {
645        _ps.setLong(1, _value.getEntityId());
646        
647        GlAccountPK gladt_gla_glaccountid = _value.getGlAccountPK();
648        if(gladt_gla_glaccountid == null)
649            _ps.setNull(2, Types.BIGINT);
650        else
651            _ps.setLong(2, gladt_gla_glaccountid.getEntityId());
652            
653        String gladt_glaccountname = _value.getGlAccountName();
654        if(gladt_glaccountname == null)
655            _ps.setNull(3, Types.VARCHAR);
656        else
657            _ps.setString(3, gladt_glaccountname);
658            
659        GlAccountPK gladt_parentglaccountid = _value.getParentGlAccountPK();
660        if(gladt_parentglaccountid == null)
661            _ps.setNull(4, Types.BIGINT);
662        else
663            _ps.setLong(4, gladt_parentglaccountid.getEntityId());
664            
665        GlAccountTypePK gladt_glatyp_glaccounttypeid = _value.getGlAccountTypePK();
666        if(gladt_glatyp_glaccounttypeid == null)
667            _ps.setNull(5, Types.BIGINT);
668        else
669            _ps.setLong(5, gladt_glatyp_glaccounttypeid.getEntityId());
670            
671        GlAccountClassPK gladt_glacls_glaccountclassid = _value.getGlAccountClassPK();
672        if(gladt_glacls_glaccountclassid == null)
673            _ps.setNull(6, Types.BIGINT);
674        else
675            _ps.setLong(6, gladt_glacls_glaccountclassid.getEntityId());
676            
677        GlAccountCategoryPK gladt_glac_glaccountcategoryid = _value.getGlAccountCategoryPK();
678        if(gladt_glac_glaccountcategoryid == null)
679            _ps.setNull(7, Types.BIGINT);
680        else
681            _ps.setLong(7, gladt_glac_glaccountcategoryid.getEntityId());
682            
683        GlResourceTypePK gladt_glrtyp_glresourcetypeid = _value.getGlResourceTypePK();
684        if(gladt_glrtyp_glresourcetypeid == null)
685            _ps.setNull(8, Types.BIGINT);
686        else
687            _ps.setLong(8, gladt_glrtyp_glresourcetypeid.getEntityId());
688            
689        CurrencyPK gladt_cur_currencyid = _value.getCurrencyPK();
690        if(gladt_cur_currencyid == null)
691            _ps.setNull(9, Types.BIGINT);
692        else
693            _ps.setLong(9, gladt_cur_currencyid.getEntityId());
694            
695        Boolean gladt_isdefault = _value.getIsDefault();
696        if(gladt_isdefault == null)
697            _ps.setNull(10, Types.BIT);
698        else
699            _ps.setInt(10, gladt_isdefault? 1: 0);
700            
701        Long gladt_fromtime = _value.getFromTime();
702        if(gladt_fromtime == null)
703            _ps.setNull(11, Types.BIGINT);
704        else
705            _ps.setLong(11, gladt_fromtime);
706            
707        Long gladt_thrutime = _value.getThruTime();
708        if(gladt_thrutime == null)
709            _ps.setNull(12, Types.BIGINT);
710        else
711            _ps.setLong(12, gladt_thrutime);
712            
713    }
714    
715    public GlAccountDetail create(GlAccountPK glAccountPK, String glAccountName, GlAccountPK parentGlAccountPK, GlAccountTypePK glAccountTypePK, GlAccountClassPK glAccountClassPK, GlAccountCategoryPK glAccountCategoryPK, GlResourceTypePK glResourceTypePK, CurrencyPK currencyPK, Boolean isDefault, Long fromTime, Long thruTime)
716            throws PersistenceDatabaseException, PersistenceNotNullException {
717        GlAccountDetailPK _pk = getNextPK();
718        GlAccountDetailValue _value = new GlAccountDetailValue(_pk, glAccountPK, glAccountName, parentGlAccountPK, glAccountTypePK, glAccountClassPK, glAccountCategoryPK, glResourceTypePK, currencyPK, isDefault, fromTime, thruTime);
719        
720        PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
721        
722        try {
723            bindForCreate(_ps, _value);
724            
725            if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
726                int _count = _ps.executeUpdate();
727                
728                if(_count != 1) {
729                    throw new PersistenceDatabaseUpdateException("insert failed, _count = " + _count);
730                }
731            } else {
732                 _ps.executeUpdate();
733            }
734            
735            session.getValueCache().put(_value);
736        } catch (SQLException se) {
737            throw new PersistenceDatabaseException(se);
738        }
739        
740        GlAccountDetail _entity = new GlAccountDetail(_value, EntityPermission.READ_ONLY);
741        session.putReadOnlyEntity(_pk, _entity);
742        
743        return _entity;
744    }
745    
746    public void create(Collection<GlAccountDetailValue> _values)
747            throws PersistenceDatabaseException, PersistenceNotNullException {
748        int _size = _values.size();
749        
750        if(_size > 0) {
751            PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
752            List<GlAccountDetailValue> _cacheValues = new ArrayList<>(_size);
753            
754            try {
755                for(GlAccountDetailValue _value : _values) {
756                    _value.setEntityId(entityIdGenerator.getNextEntityId());
757                    bindForCreate(_ps, _value);
758                    
759                    _ps.addBatch();
760                    
761                    _cacheValues.add(_value);
762                }
763                
764                if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
765                    int[] _counts = _ps.executeBatch();
766                    
767                    for(int _countOffset = 0 ; _countOffset < _size ; _countOffset++) {
768                        if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
769                            throw new PersistenceDatabaseUpdateException("batch insert failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
770                        }
771                    }
772                } else {
773                     _ps.executeBatch();
774                }
775                
776                _ps.clearBatch();
777            } catch (SQLException se) {
778                throw new PersistenceDatabaseException(se);
779            }
780            
781            _cacheValues.forEach((_cacheValue) -> {
782                GlAccountDetail _cacheEntity = new GlAccountDetail(_cacheValue, EntityPermission.READ_ONLY);
783                
784                session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity);
785            });
786        }
787    }
788    
789    private boolean bindForStore(PreparedStatement _ps, GlAccountDetailValue _value)
790            throws SQLException {
791        boolean _hasBeenModified = _value.hasBeenModified();
792        
793        if(_hasBeenModified) {
794            GlAccountPK gladt_gla_glaccountid = _value.getGlAccountPK();
795            if(gladt_gla_glaccountid == null)
796                _ps.setNull(1, Types.BIGINT);
797            else
798                _ps.setLong(1, gladt_gla_glaccountid.getEntityId());
799            
800            String gladt_glaccountname = _value.getGlAccountName();
801            if(gladt_glaccountname == null)
802                _ps.setNull(2, Types.VARCHAR);
803            else
804                _ps.setString(2, gladt_glaccountname);
805            
806            GlAccountPK gladt_parentglaccountid = _value.getParentGlAccountPK();
807            if(gladt_parentglaccountid == null)
808                _ps.setNull(3, Types.BIGINT);
809            else
810                _ps.setLong(3, gladt_parentglaccountid.getEntityId());
811            
812            GlAccountTypePK gladt_glatyp_glaccounttypeid = _value.getGlAccountTypePK();
813            if(gladt_glatyp_glaccounttypeid == null)
814                _ps.setNull(4, Types.BIGINT);
815            else
816                _ps.setLong(4, gladt_glatyp_glaccounttypeid.getEntityId());
817            
818            GlAccountClassPK gladt_glacls_glaccountclassid = _value.getGlAccountClassPK();
819            if(gladt_glacls_glaccountclassid == null)
820                _ps.setNull(5, Types.BIGINT);
821            else
822                _ps.setLong(5, gladt_glacls_glaccountclassid.getEntityId());
823            
824            GlAccountCategoryPK gladt_glac_glaccountcategoryid = _value.getGlAccountCategoryPK();
825            if(gladt_glac_glaccountcategoryid == null)
826                _ps.setNull(6, Types.BIGINT);
827            else
828                _ps.setLong(6, gladt_glac_glaccountcategoryid.getEntityId());
829            
830            GlResourceTypePK gladt_glrtyp_glresourcetypeid = _value.getGlResourceTypePK();
831            if(gladt_glrtyp_glresourcetypeid == null)
832                _ps.setNull(7, Types.BIGINT);
833            else
834                _ps.setLong(7, gladt_glrtyp_glresourcetypeid.getEntityId());
835            
836            CurrencyPK gladt_cur_currencyid = _value.getCurrencyPK();
837            if(gladt_cur_currencyid == null)
838                _ps.setNull(8, Types.BIGINT);
839            else
840                _ps.setLong(8, gladt_cur_currencyid.getEntityId());
841            
842            Boolean gladt_isdefault = _value.getIsDefault();
843            if(gladt_isdefault == null)
844                _ps.setNull(9, Types.BIT);
845            else
846                _ps.setInt(9, gladt_isdefault? 1: 0);
847            
848            Long gladt_fromtime = _value.getFromTime();
849            if(gladt_fromtime == null)
850                _ps.setNull(10, Types.BIGINT);
851            else
852                _ps.setLong(10, gladt_fromtime);
853            
854            Long gladt_thrutime = _value.getThruTime();
855            if(gladt_thrutime == null)
856                _ps.setNull(11, Types.BIGINT);
857            else
858                _ps.setLong(11, gladt_thrutime);
859            
860            _ps.setLong(12, _value.getPrimaryKey().getEntityId());
861            
862            _value.clearHasBeenModified();
863        }
864        
865        return _hasBeenModified;
866    }
867    
868    @Override
869    public void store(GlAccountDetail entity)
870            throws PersistenceDatabaseException {
871        PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
872        
873        try {
874            GlAccountDetailValue _value = entity.getGlAccountDetailValue();
875            
876            if(bindForStore(_ps, _value)) {
877                if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
878                    int _count = _ps.executeUpdate();
879                    
880                    if(_count != 1) {
881                        throw new PersistenceDatabaseUpdateException("update failed, _count = " + _count);
882                    }
883                } else {
884                     _ps.executeUpdate();
885                }
886                
887                session.getValueCache().put(_value);
888            }
889        } catch (SQLException se) {
890            throw new PersistenceDatabaseException(se);
891        }
892    }
893    
894    @Override
895    public void store(Collection<GlAccountDetail> entities)
896            throws PersistenceDatabaseException {
897        if(entities.size() > 0) {
898            PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
899            int _modifiedEntities = 0;
900            
901            try {
902                for(GlAccountDetail entity : entities) {
903                    if(bindForStore(_ps, entity.getGlAccountDetailValue())) {
904                        _ps.addBatch();
905                        _modifiedEntities++;
906                    }
907                }
908                
909                if(_modifiedEntities != 0) {
910                    if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
911                        int[] _counts = _ps.executeBatch();
912                        
913                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
914                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
915                                throw new PersistenceDatabaseUpdateException("batch update failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
916                            }
917                        }
918                    } else {
919                         _ps.executeBatch();
920                    }
921                    
922                    _ps.clearBatch();
923                    
924                    entities.forEach((entity) -> {
925                        session.getValueCache().put(entity.getGlAccountDetailValue());
926                    });
927                }
928            } catch (SQLException se) {
929                throw new PersistenceDatabaseException(se);
930            }
931        }
932    }
933    
934    @Override
935    public void remove(GlAccountDetail entity)
936            throws PersistenceDatabaseException {
937        remove(entity.getPrimaryKey());
938    }
939    
940    @Override
941    public void remove(GlAccountDetailPK pk)
942            throws PersistenceDatabaseException {
943        PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
944        long _entityId = pk.getEntityId();
945        
946        try {
947            _ps.setLong(1, _entityId);
948            
949            if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
950                int _count = _ps.executeUpdate();
951                
952                if(_count != 1) {
953                    throw new PersistenceDatabaseUpdateException("remove failed, _count = " + _count);
954                }
955            } else {
956                 _ps.executeUpdate();
957            }
958            
959            session.getValueCache().remove(pk);
960        } catch (SQLException se) {
961            throw new PersistenceDatabaseException(se);
962        }
963        
964        session.removed(pk, false);
965    }
966    
967    @Override
968    public void remove(Collection<GlAccountDetailPK> pks)
969            throws PersistenceDatabaseException {
970        if(pks.size() > 0) {
971            PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
972            int _modifiedEntities = 0;
973            
974            try {
975                for(GlAccountDetailPK pk : pks) {
976                    long _entityId = pk.getEntityId();
977                    
978                    _ps.setLong(1, _entityId);
979                    
980                    _ps.addBatch();
981                    _modifiedEntities++;
982                }
983                
984                if(_modifiedEntities != 0) {
985                    if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
986                        int[] _counts = _ps.executeBatch();
987                        
988                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
989                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
990                                throw new PersistenceDatabaseUpdateException("batch remove failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
991                            }
992                        }
993                    } else {
994                        _ps.executeBatch();
995                    }
996                    
997                    _ps.clearBatch();
998                    
999                    pks.forEach((pk) -> {
1000                        session.getValueCache().remove(pk);
1001                    });
1002                }
1003            } catch (SQLException se) {
1004                throw new PersistenceDatabaseException(se);
1005            }
1006            
1007            pks.forEach((pk) -> {
1008                session.removed(pk, true);
1009            });
1010        }
1011    }
1012    
1013    @Override
1014    public boolean validPK(GlAccountDetailPK pk)
1015            throws PersistenceDatabaseException {
1016        boolean valid = false;
1017        PreparedStatement _ps = session.prepareStatement(SQL_VALID);
1018        ResultSet _rs = null;
1019        
1020        try {
1021            _ps.setLong(1, pk.getEntityId());
1022            
1023            _rs = _ps.executeQuery();
1024            if(_rs.next()) {
1025                long _count = _rs.getLong(1);
1026                if(_rs.wasNull())
1027                    _count = 0;
1028                
1029                if(_count == 1)
1030                    valid = true;
1031            }
1032        } catch (SQLException se) {
1033            throw new PersistenceDatabaseException(se);
1034        } finally {
1035            if(_rs != null) {
1036                try {
1037                    _rs.close();
1038                } catch (SQLException se) {
1039                    // do nothing
1040                }
1041            }
1042        }
1043        
1044        return valid;
1045    }
1046    
1047}