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