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