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