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