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