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