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