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