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