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