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