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