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