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