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