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