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