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