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