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