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