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