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