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