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