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