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