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