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