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