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