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