001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016// Generated File -- DO NOT EDIT BY HAND
017// --------------------------------------------------------------------------------
018
019/**
020 * 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 com.echothree.util.server.persistence.ThreadSession;
046import java.sql.PreparedStatement;
047import java.sql.ResultSet;
048import java.sql.SQLException;
049import java.sql.Types;
050import java.io.ByteArrayInputStream;
051import java.io.StringReader;
052import java.util.ArrayList;
053import java.util.Collection;
054import java.util.HashSet;
055import java.util.List;
056import java.util.Map;
057import java.util.Set;
058import javax.enterprise.context.ApplicationScoped;
059import javax.enterprise.inject.spi.CDI;
060import org.apache.commons.logging.Log;
061import org.apache.commons.logging.LogFactory;
062
063@ApplicationScoped
064public class PersonFactory
065        implements BaseFactory<PersonPK, Person> {
066    
067    //final private static Log log = LogFactory.getLog(PersonFactory.class);
068    
069    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 = ?";
070    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";
071    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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
072    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 = ?";
073    final private static String SQL_DELETE = "DELETE FROM people WHERE peop_personid = ?";
074    final private static String SQL_VALID = "SELECT COUNT(*) FROM people WHERE peop_personid = ?";
075    
076    final private static String PK_COLUMN = "peop_personid";
077    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";
078    final public static String TABLE_NAME = "people";
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 getPKColumn() {
106        return PK_COLUMN;
107    }
108    
109    @Override
110    public String getAllColumns() {
111        return ALL_COLUMNS;
112    }
113    
114    @Override
115    public String getTableName() {
116        return TABLE_NAME;
117    }
118    
119    @Override
120    public String getComponentVendorName() {
121        return PersonConstants.COMPONENT_VENDOR_NAME;
122    }
123    
124    @Override
125    public String getEntityTypeName() {
126        return PersonConstants.ENTITY_TYPE_NAME;
127    }
128    
129    public PreparedStatement prepareStatement(String query) {
130        return ThreadSession.currentSession().prepareStatement(PersonFactory.class, query);
131    }
132    
133    public PersonPK getNextPK() {
134        return new PersonPK(entityIdGenerator.getNextEntityId());
135    }
136    
137    public Set<PersonPK> getPKsFromResultSetAsSet(ResultSet rs)
138            throws PersistenceDatabaseException {
139        Set<PersonPK> _result = new HashSet<>();
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 java.util.List<PersonPK> getPKsFromResultSetAsList(ResultSet rs)
153            throws PersistenceDatabaseException {
154        java.util.List<PersonPK> _result = new ArrayList<>();
155        
156        try {
157            while(rs.next()) {
158                _result.add(getPKFromResultSet(rs));
159            }
160        } catch (SQLException se) {
161            throw new PersistenceDatabaseException(se);
162        }
163        
164        return _result;
165    }
166    
167    public PersonPK getPKFromResultSet(ResultSet rs)
168            throws PersistenceDatabaseException {
169        PersonPK _result;
170        
171        try {
172            long peop_personid = rs.getLong(PEOP_PERSONID);
173            Long _entityId = rs.wasNull() ? null : peop_personid;
174            
175            _result = new PersonPK(_entityId);
176        } catch (SQLException se) {
177            throw new PersistenceDatabaseException(se);
178        }
179        
180        return _result;
181    }
182    
183    public java.util.List<PersonValue> getValuesFromPKs(Session session, Collection<PersonPK> pks)
184            throws PersistenceDatabaseException {
185        java.util.List<PersonValue> _values = new ArrayList<>(pks.size());
186        
187        for(PersonPK _pk: pks) {
188            _values.add(getValueFromPK(session, _pk));
189        }
190        
191        return _values;
192    }
193    
194    public PersonValue getValueFromPK(Session session, PersonPK pk)
195            throws PersistenceDatabaseException {
196        PersonValue _value;
197        
198        // See if we already have the entity in the session cache
199        Person _entity = (Person)session.getEntity(pk);
200        if(_entity == null)
201            _value = getEntityFromPK(session, EntityPermission.READ_ONLY, pk).getPersonValue();
202        else
203            _value = _entity.getPersonValue();
204        
205        return _value;
206    }
207    
208    public java.util.List<PersonValue> getValuesFromResultSet(Session session, ResultSet rs)
209            throws PersistenceDatabaseException {
210        java.util.List<PersonValue> _result = new ArrayList<>();
211        
212        try {
213            while(rs.next()) {
214                _result.add(getValueFromResultSet(session, rs));
215            }
216        } catch (SQLException se) {
217            throw new PersistenceDatabaseException(se);
218        }
219        
220        return _result;
221    }
222    
223    public PersonValue getValueFromResultSet(Session session, ResultSet rs)
224            throws PersistenceDatabaseException {
225        PersonValue _value;
226        
227        try {
228            Long peop_personid = rs.getLong(PEOP_PERSONID);
229            PersonPK _pk = new PersonPK(peop_personid);
230            
231            // See if we already have the entity in the session cache
232            Person _entity = (Person)session.getEntity(_pk);
233            
234            if(_entity == null) {
235                Long peop_par_partyid = rs.getLong(PEOP_PAR_PARTYID);
236                if(rs.wasNull())
237                    peop_par_partyid = null;
238                
239                Long peop_pert_personaltitleid = rs.getLong(PEOP_PERT_PERSONALTITLEID);
240                if(rs.wasNull())
241                    peop_pert_personaltitleid = null;
242                
243                String peop_firstname = rs.getString(PEOP_FIRSTNAME);
244                if(rs.wasNull())
245                    peop_firstname = null;
246                
247                String peop_firstnamesdx = rs.getString(PEOP_FIRSTNAMESDX);
248                if(rs.wasNull())
249                    peop_firstnamesdx = null;
250                
251                String peop_middlename = rs.getString(PEOP_MIDDLENAME);
252                if(rs.wasNull())
253                    peop_middlename = null;
254                
255                String peop_middlenamesdx = rs.getString(PEOP_MIDDLENAMESDX);
256                if(rs.wasNull())
257                    peop_middlenamesdx = null;
258                
259                String peop_lastname = rs.getString(PEOP_LASTNAME);
260                if(rs.wasNull())
261                    peop_lastname = null;
262                
263                String peop_lastnamesdx = rs.getString(PEOP_LASTNAMESDX);
264                if(rs.wasNull())
265                    peop_lastnamesdx = null;
266                
267                Long peop_nsfx_namesuffixid = rs.getLong(PEOP_NSFX_NAMESUFFIXID);
268                if(rs.wasNull())
269                    peop_nsfx_namesuffixid = null;
270                
271                Long peop_fromtime = rs.getLong(PEOP_FROMTIME);
272                if(rs.wasNull())
273                    peop_fromtime = null;
274                
275                Long peop_thrutime = rs.getLong(PEOP_THRUTIME);
276                if(rs.wasNull())
277                    peop_thrutime = null;
278                
279                _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);
280            } else
281                _value = _entity.getPersonValue();
282        } catch (SQLException se) {
283            throw new PersistenceDatabaseException(se);
284        }
285        
286        return _value;
287    }
288    
289    public java.util.List<Person> getEntitiesFromPKs(EntityPermission entityPermission, Collection<PersonPK> pks)
290            throws PersistenceDatabaseException {
291        return getEntitiesFromPKs(ThreadSession.currentSession(), entityPermission, pks);
292    }
293    
294    public java.util.List<Person> getEntitiesFromPKs(Session session, EntityPermission entityPermission, Collection<PersonPK> pks)
295            throws PersistenceDatabaseException {
296        java.util.List<Person> _entities = new ArrayList<>(pks.size());
297        
298        for(PersonPK _pk: pks) {
299            _entities.add(getEntityFromPK(session, entityPermission, _pk));
300        }
301        
302        return _entities;
303    }
304    
305    public Person getEntityFromValue(EntityPermission entityPermission, PersonValue value) {
306        return getEntityFromPK(ThreadSession.currentSession(), entityPermission, value.getPrimaryKey());
307    }
308    
309    public Person getEntityFromValue(Session session, EntityPermission entityPermission, PersonValue value) {
310        return getEntityFromPK(session, entityPermission, value.getPrimaryKey());
311    }
312    
313    public Person getEntityFromPK(EntityPermission entityPermission, PersonPK pk)
314            throws PersistenceDatabaseException {
315        return getEntityFromPK(ThreadSession.currentSession(), entityPermission, pk);
316    }
317    
318    public Person getEntityFromCache(Session session, PersonPK pk) {
319        PersonValue _value = (PersonValue)session.getValueCache().get(pk);
320    
321        return _value == null ? null : new Person(_value, EntityPermission.READ_ONLY);
322    }
323    
324    public Person getEntityFromPK(Session session, EntityPermission entityPermission, PersonPK pk)
325            throws PersistenceDatabaseException {
326        Person _entity;
327        
328        // See if we already have the entity in the session cache
329        _entity = (Person)session.getEntity(pk);
330        if(_entity != null) {
331            // If the requested permission is READ_WRITE, and the cached permission is
332            // READ_ONLY, then pretend that the cached object wasn't found, and create
333            // a new entity that is READ_WRITE.
334            if(entityPermission.equals(EntityPermission.READ_WRITE)) {
335                if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
336                    _entity = null;
337            }
338        }
339        
340        if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
341            _entity = getEntityFromCache(session, pk);
342        }
343        
344        if(_entity == null) {
345            PreparedStatement _ps = session.prepareStatement(entityPermission.equals(EntityPermission.READ_ONLY)? SQL_SELECT_READ_ONLY: SQL_SELECT_READ_WRITE);
346            long _entityId = pk.getEntityId();
347            ResultSet _rs = null;
348            
349            try {
350                _ps.setLong(1, _entityId);
351                _rs = _ps.executeQuery();
352                if(_rs.next()) {
353                    _entity = getEntityFromResultSet(session, entityPermission, _rs);
354                }
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        
368        return _entity;
369    }
370    
371    public Set<PersonPK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params)
372            throws PersistenceDatabaseException {
373        Set<PersonPK> _pks;
374        ResultSet _rs = null;
375        
376        try {
377            if(params.length != 0) {
378                Session.setQueryParams(ps, params);
379            }
380            
381            _rs = ps.executeQuery();
382            _pks = getPKsFromResultSetAsSet(_rs);
383            _rs.close();
384        } catch (SQLException se) {
385            throw new PersistenceDatabaseException(se);
386        } finally {
387            if(_rs != null) {
388                try {
389                    _rs.close();
390                } catch (SQLException se) {
391                    // do nothing
392                }
393            }
394        }
395        
396        return _pks;
397    }
398    
399    public java.util.List<PersonPK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params)
400            throws PersistenceDatabaseException {
401        java.util.List<PersonPK> _pks;
402        ResultSet _rs = null;
403        
404        try {
405            if(params.length != 0) {
406                Session.setQueryParams(ps, params);
407            }
408            
409            _rs = ps.executeQuery();
410            _pks = getPKsFromResultSetAsList(_rs);
411            _rs.close();
412        } catch (SQLException se) {
413            throw new PersistenceDatabaseException(se);
414        } finally {
415            if(_rs != null) {
416                try {
417                    _rs.close();
418                } catch (SQLException se) {
419                    // do nothing
420                }
421            }
422        }
423        
424        return _pks;
425    }
426    
427    public PersonPK getPKFromQuery(PreparedStatement ps, final Object... params)
428            throws PersistenceDatabaseException {
429        PersonPK _pk = null;
430        ResultSet _rs = null;
431        
432        try {
433            if(params.length != 0) {
434                Session.setQueryParams(ps, params);
435            }
436            
437            _rs = ps.executeQuery();
438            if(_rs.next()) {
439                _pk = getPKFromResultSet(_rs);
440            }
441            _rs.close();
442        } catch (SQLException se) {
443            throw new PersistenceDatabaseException(se);
444        } finally {
445            if(_rs != null) {
446                try {
447                    _rs.close();
448                } catch (SQLException se) {
449                    // do nothing
450                }
451            }
452        }
453        
454        return _pk;
455    }
456    
457    public java.util.List<Person> getEntitiesFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
458            throws PersistenceDatabaseException {
459        PreparedStatement ps = session.prepareStatement(PersonFactory.class, queryMap.get(entityPermission));
460        
461        return getEntitiesFromQuery(session, entityPermission, ps, params);
462    }
463    
464    public java.util.List<Person> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
465            throws PersistenceDatabaseException {
466        Session session = ThreadSession.currentSession();
467        PreparedStatement ps = session.prepareStatement(PersonFactory.class, queryMap.get(entityPermission));
468        
469        return getEntitiesFromQuery(session, entityPermission, ps, params);
470    }
471    
472    public java.util.List<Person> getEntitiesFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
473            throws PersistenceDatabaseException {
474        PreparedStatement ps = session.prepareStatement(PersonFactory.class, queryMap.get(entityPermission));
475        
476        return getEntitiesFromQuery(session, entityPermission, ps);
477    }
478    
479    public java.util.List<Person> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
480            throws PersistenceDatabaseException {
481        Session session = ThreadSession.currentSession();
482        PreparedStatement ps = session.prepareStatement(PersonFactory.class, queryMap.get(entityPermission));
483        
484        return getEntitiesFromQuery(session, entityPermission, ps);
485    }
486    
487    public java.util.List<Person> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps)
488            throws PersistenceDatabaseException {
489        return getEntitiesFromQuery(ThreadSession.currentSession(), entityPermission, ps);
490    }
491    
492    public java.util.List<Person> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
493            throws PersistenceDatabaseException {
494        return getEntitiesFromQuery(ThreadSession.currentSession(), entityPermission, ps, params);
495    }
496    
497    public java.util.List<Person> getEntitiesFromQuery(Session session, EntityPermission entityPermission, PreparedStatement ps, final Object... params)
498            throws PersistenceDatabaseException {
499        java.util.List<Person> _entities;
500        ResultSet _rs = null;
501        
502        try {
503            if(params.length != 0) {
504                Session.setQueryParams(ps, params);
505            }
506            
507            _rs = ps.executeQuery();
508            _entities = getEntitiesFromResultSet(session, entityPermission, _rs);
509            _rs.close();
510        } catch (SQLException se) {
511            throw new PersistenceDatabaseException(se);
512        } finally {
513            if(_rs != null) {
514                try {
515                    _rs.close();
516                } catch (SQLException se) {
517                    // do nothing
518                }
519            }
520        }
521        
522        return _entities;
523    }
524    
525    public Person getEntityFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
526            throws PersistenceDatabaseException {
527        PreparedStatement ps = session.prepareStatement(PersonFactory.class, queryMap.get(entityPermission));
528        
529        return getEntityFromQuery(session, entityPermission, ps, params);
530    }
531    
532    public Person getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
533            throws PersistenceDatabaseException {
534        Session session = ThreadSession.currentSession();
535        PreparedStatement ps = session.prepareStatement(PersonFactory.class, queryMap.get(entityPermission));
536        
537        return getEntityFromQuery(session, entityPermission, ps, params);
538    }
539    
540    public Person getEntityFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
541            throws PersistenceDatabaseException {
542        PreparedStatement ps = session.prepareStatement(PersonFactory.class, queryMap.get(entityPermission));
543        
544        return getEntityFromQuery(session, entityPermission, ps);
545    }
546    
547    public Person getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
548            throws PersistenceDatabaseException {
549        Session session = ThreadSession.currentSession();
550        PreparedStatement ps = session.prepareStatement(PersonFactory.class, queryMap.get(entityPermission));
551        
552        return getEntityFromQuery(session, entityPermission, ps);
553    }
554    
555    public Person getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps)
556            throws PersistenceDatabaseException {
557        return getEntityFromQuery(ThreadSession.currentSession(), entityPermission, ps);
558    }
559    
560    public Person getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
561            throws PersistenceDatabaseException {
562        return getEntityFromQuery(ThreadSession.currentSession(), entityPermission, ps, params);
563    }
564    
565    public Person getEntityFromQuery(Session session, EntityPermission entityPermission, PreparedStatement ps, final Object... params)
566            throws PersistenceDatabaseException {
567        Person _entity = null;
568        ResultSet _rs = null;
569        
570        try {
571            if(params.length != 0) {
572                Session.setQueryParams(ps, params);
573            }
574            
575            _rs = ps.executeQuery();
576            if(_rs.next()) {
577                _entity = getEntityFromResultSet(session, entityPermission, _rs);
578            }
579            _rs.close();
580        } catch (SQLException se) {
581            throw new PersistenceDatabaseException(se);
582        } finally {
583            if(_rs != null) {
584                try {
585                    _rs.close();
586                } catch (SQLException se) {
587                    // do nothing
588                }
589            }
590        }
591        
592        return _entity;
593    }
594    
595    public java.util.List<Person> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs)
596            throws PersistenceDatabaseException {
597        return getEntitiesFromResultSet(ThreadSession.currentSession(), entityPermission, rs);
598    }
599    
600    public java.util.List<Person> getEntitiesFromResultSet(Session session, EntityPermission entityPermission, ResultSet rs)
601            throws PersistenceDatabaseException {
602        java.util.List<Person> _result = new ArrayList<>();
603        
604        try {
605            while(rs.next()) {
606                _result.add(getEntityFromResultSet(session, entityPermission, rs));
607            }
608        } catch (SQLException se) {
609            throw new PersistenceDatabaseException(se);
610        }
611        
612        return _result;
613    }
614    
615    public Person getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs)
616            throws PersistenceDatabaseException {
617        return getEntityFromResultSet(ThreadSession.currentSession(), entityPermission, rs);
618    }
619    
620    public Person getEntityFromResultSet(Session session, EntityPermission entityPermission, ResultSet rs)
621            throws PersistenceDatabaseException {
622        Person _entity;
623        
624        try {
625            Long peop_personid = rs.getLong(PEOP_PERSONID);
626            PersonPK _pk = new PersonPK(peop_personid);
627            
628            // See if we already have the entity in the session cache
629            _entity = (Person)session.getEntity(_pk);
630            if(_entity != null) {
631                // If the requested permission is READ_WRITE, and the cached permission is
632                // READ_ONLY, then pretend that the cached object wasn't found, and create
633                // a new entity that is READ_WRITE.
634                if(entityPermission.equals(EntityPermission.READ_WRITE)) {
635                    if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
636                        _entity = null;
637                }
638            }
639            boolean foundInSessionCache = _entity != null;
640            
641            if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
642                _entity = getEntityFromCache(session, _pk);
643            }
644            
645            if(_entity == null) {
646                Long peop_par_partyid = rs.getLong(PEOP_PAR_PARTYID);
647                if(rs.wasNull())
648                    peop_par_partyid = null;
649                
650                Long peop_pert_personaltitleid = rs.getLong(PEOP_PERT_PERSONALTITLEID);
651                if(rs.wasNull())
652                    peop_pert_personaltitleid = null;
653                
654                String peop_firstname = rs.getString(PEOP_FIRSTNAME);
655                if(rs.wasNull())
656                    peop_firstname = null;
657                
658                String peop_firstnamesdx = rs.getString(PEOP_FIRSTNAMESDX);
659                if(rs.wasNull())
660                    peop_firstnamesdx = null;
661                
662                String peop_middlename = rs.getString(PEOP_MIDDLENAME);
663                if(rs.wasNull())
664                    peop_middlename = null;
665                
666                String peop_middlenamesdx = rs.getString(PEOP_MIDDLENAMESDX);
667                if(rs.wasNull())
668                    peop_middlenamesdx = null;
669                
670                String peop_lastname = rs.getString(PEOP_LASTNAME);
671                if(rs.wasNull())
672                    peop_lastname = null;
673                
674                String peop_lastnamesdx = rs.getString(PEOP_LASTNAMESDX);
675                if(rs.wasNull())
676                    peop_lastnamesdx = null;
677                
678                Long peop_nsfx_namesuffixid = rs.getLong(PEOP_NSFX_NAMESUFFIXID);
679                if(rs.wasNull())
680                    peop_nsfx_namesuffixid = null;
681                
682                Long peop_fromtime = rs.getLong(PEOP_FROMTIME);
683                if(rs.wasNull())
684                    peop_fromtime = null;
685                
686                Long peop_thrutime = rs.getLong(PEOP_THRUTIME);
687                if(rs.wasNull())
688                    peop_thrutime = null;
689                
690                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);
691                _entity = new Person(_value, entityPermission);
692            }
693            
694            if(!foundInSessionCache) {
695                if(entityPermission.equals(EntityPermission.READ_ONLY)) {
696                    session.putReadOnlyEntity(_pk, _entity);
697                    session.getValueCache().put(_entity.getPersonValue());
698                } else {
699                    session.putReadWriteEntity(_pk, _entity);
700                }
701            }
702        } catch (SQLException se) {
703            throw new PersistenceDatabaseException(se);
704        }
705        
706        return _entity;
707    }
708    
709    public Person create(Session session, Party party, PersonalTitle personalTitle, String firstName, String firstNameSdx, String middleName, String middleNameSdx, String lastName, String lastNameSdx, NameSuffix nameSuffix, Long fromTime, Long thruTime)
710            throws PersistenceDatabaseException, PersistenceNotNullException {
711        return create(session, party == null ? null : party.getPrimaryKey(), personalTitle == null ? null : personalTitle.getPrimaryKey(), firstName, firstNameSdx, middleName, middleNameSdx, lastName, lastNameSdx, nameSuffix == null ? null : nameSuffix.getPrimaryKey(), fromTime, thruTime);
712    }
713    
714    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)
715            throws PersistenceDatabaseException, PersistenceNotNullException {
716        return create(ThreadSession.currentSession(), party == null ? null : party.getPrimaryKey(), personalTitle == null ? null : personalTitle.getPrimaryKey(), firstName, firstNameSdx, middleName, middleNameSdx, lastName, lastNameSdx, nameSuffix == null ? null : nameSuffix.getPrimaryKey(), fromTime, thruTime);
717    }
718    
719    private void bindForCreate(PreparedStatement _ps, PersonValue _value)
720            throws SQLException {
721        _ps.setLong(1, _value.getEntityId());
722        
723        PartyPK peop_par_partyid = _value.getPartyPK();
724        if(peop_par_partyid == null)
725            _ps.setNull(2, Types.BIGINT);
726        else
727            _ps.setLong(2, peop_par_partyid.getEntityId());
728            
729        PersonalTitlePK peop_pert_personaltitleid = _value.getPersonalTitlePK();
730        if(peop_pert_personaltitleid == null)
731            _ps.setNull(3, Types.BIGINT);
732        else
733            _ps.setLong(3, peop_pert_personaltitleid.getEntityId());
734            
735        String peop_firstname = _value.getFirstName();
736        if(peop_firstname == null)
737            _ps.setNull(4, Types.VARCHAR);
738        else
739            _ps.setString(4, peop_firstname);
740            
741        String peop_firstnamesdx = _value.getFirstNameSdx();
742        if(peop_firstnamesdx == null)
743            _ps.setNull(5, Types.VARCHAR);
744        else
745            _ps.setString(5, peop_firstnamesdx);
746            
747        String peop_middlename = _value.getMiddleName();
748        if(peop_middlename == null)
749            _ps.setNull(6, Types.VARCHAR);
750        else
751            _ps.setString(6, peop_middlename);
752            
753        String peop_middlenamesdx = _value.getMiddleNameSdx();
754        if(peop_middlenamesdx == null)
755            _ps.setNull(7, Types.VARCHAR);
756        else
757            _ps.setString(7, peop_middlenamesdx);
758            
759        String peop_lastname = _value.getLastName();
760        if(peop_lastname == null)
761            _ps.setNull(8, Types.VARCHAR);
762        else
763            _ps.setString(8, peop_lastname);
764            
765        String peop_lastnamesdx = _value.getLastNameSdx();
766        if(peop_lastnamesdx == null)
767            _ps.setNull(9, Types.VARCHAR);
768        else
769            _ps.setString(9, peop_lastnamesdx);
770            
771        NameSuffixPK peop_nsfx_namesuffixid = _value.getNameSuffixPK();
772        if(peop_nsfx_namesuffixid == null)
773            _ps.setNull(10, Types.BIGINT);
774        else
775            _ps.setLong(10, peop_nsfx_namesuffixid.getEntityId());
776            
777        Long peop_fromtime = _value.getFromTime();
778        if(peop_fromtime == null)
779            _ps.setNull(11, Types.BIGINT);
780        else
781            _ps.setLong(11, peop_fromtime);
782            
783        Long peop_thrutime = _value.getThruTime();
784        if(peop_thrutime == null)
785            _ps.setNull(12, Types.BIGINT);
786        else
787            _ps.setLong(12, peop_thrutime);
788            
789    }
790    
791    public Person create(Session session, PartyPK partyPK, PersonalTitlePK personalTitlePK, String firstName, String firstNameSdx, String middleName, String middleNameSdx, String lastName, String lastNameSdx, NameSuffixPK nameSuffixPK, Long fromTime, Long thruTime)
792            throws PersistenceDatabaseException, PersistenceNotNullException {
793        PersonPK _pk = getNextPK();
794        PersonValue _value = new PersonValue(_pk, partyPK, personalTitlePK, firstName, firstNameSdx, middleName, middleNameSdx, lastName, lastNameSdx, nameSuffixPK, fromTime, thruTime);
795        
796        PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
797        
798        try {
799            bindForCreate(_ps, _value);
800            
801            if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
802                int _count = _ps.executeUpdate();
803                
804                if(_count != 1) {
805                    throw new PersistenceDatabaseUpdateException("insert failed, _count = " + _count);
806                }
807            } else {
808                 _ps.executeUpdate();
809            }
810            
811            session.getValueCache().put(_value);
812        } catch (SQLException se) {
813            throw new PersistenceDatabaseException(se);
814        }
815        
816        Person _entity = new Person(_value, EntityPermission.READ_ONLY);
817        session.putReadOnlyEntity(_pk, _entity);
818        
819        return _entity;
820    }
821    
822    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)
823            throws PersistenceDatabaseException, PersistenceNotNullException {
824        return create(ThreadSession.currentSession(), partyPK, personalTitlePK, firstName, firstNameSdx, middleName, middleNameSdx, lastName, lastNameSdx, nameSuffixPK, fromTime, thruTime);
825    }
826    
827    public void create(Session session, Collection<PersonValue> _values)
828            throws PersistenceDatabaseException, PersistenceNotNullException {
829        int _size = _values.size();
830        
831        if(_size > 0) {
832            PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
833            List<PersonValue> _cacheValues = new ArrayList<>(_size);
834            
835            try {
836                for(PersonValue _value : _values) {
837                    _value.setEntityId(entityIdGenerator.getNextEntityId());
838                    bindForCreate(_ps, _value);
839                    
840                    _ps.addBatch();
841                    
842                    _cacheValues.add(_value);
843                }
844                
845                if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
846                    int[] _counts = _ps.executeBatch();
847                    
848                    for(int _countOffset = 0 ; _countOffset < _size ; _countOffset++) {
849                        if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
850                            throw new PersistenceDatabaseUpdateException("batch insert failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
851                        }
852                    }
853                } else {
854                     _ps.executeBatch();
855                }
856                
857                _ps.clearBatch();
858            } catch (SQLException se) {
859                throw new PersistenceDatabaseException(se);
860            }
861            
862            _cacheValues.forEach((_cacheValue) -> {
863                Person _cacheEntity = new Person(_cacheValue, EntityPermission.READ_ONLY);
864                
865                session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity);
866            });
867        }
868    }
869    
870    public void create(Collection<PersonValue> _values)
871            throws PersistenceDatabaseException, PersistenceNotNullException {
872        create(ThreadSession.currentSession(), _values);
873    }
874    
875    private boolean bindForStore(PreparedStatement _ps, PersonValue _value)
876            throws SQLException {
877        boolean _hasBeenModified = _value.hasBeenModified();
878        
879        if(_hasBeenModified) {
880            PartyPK peop_par_partyid = _value.getPartyPK();
881            if(peop_par_partyid == null)
882                _ps.setNull(1, Types.BIGINT);
883            else
884                _ps.setLong(1, peop_par_partyid.getEntityId());
885            
886            PersonalTitlePK peop_pert_personaltitleid = _value.getPersonalTitlePK();
887            if(peop_pert_personaltitleid == null)
888                _ps.setNull(2, Types.BIGINT);
889            else
890                _ps.setLong(2, peop_pert_personaltitleid.getEntityId());
891            
892            String peop_firstname = _value.getFirstName();
893            if(peop_firstname == null)
894                _ps.setNull(3, Types.VARCHAR);
895            else
896                _ps.setString(3, peop_firstname);
897            
898            String peop_firstnamesdx = _value.getFirstNameSdx();
899            if(peop_firstnamesdx == null)
900                _ps.setNull(4, Types.VARCHAR);
901            else
902                _ps.setString(4, peop_firstnamesdx);
903            
904            String peop_middlename = _value.getMiddleName();
905            if(peop_middlename == null)
906                _ps.setNull(5, Types.VARCHAR);
907            else
908                _ps.setString(5, peop_middlename);
909            
910            String peop_middlenamesdx = _value.getMiddleNameSdx();
911            if(peop_middlenamesdx == null)
912                _ps.setNull(6, Types.VARCHAR);
913            else
914                _ps.setString(6, peop_middlenamesdx);
915            
916            String peop_lastname = _value.getLastName();
917            if(peop_lastname == null)
918                _ps.setNull(7, Types.VARCHAR);
919            else
920                _ps.setString(7, peop_lastname);
921            
922            String peop_lastnamesdx = _value.getLastNameSdx();
923            if(peop_lastnamesdx == null)
924                _ps.setNull(8, Types.VARCHAR);
925            else
926                _ps.setString(8, peop_lastnamesdx);
927            
928            NameSuffixPK peop_nsfx_namesuffixid = _value.getNameSuffixPK();
929            if(peop_nsfx_namesuffixid == null)
930                _ps.setNull(9, Types.BIGINT);
931            else
932                _ps.setLong(9, peop_nsfx_namesuffixid.getEntityId());
933            
934            Long peop_fromtime = _value.getFromTime();
935            if(peop_fromtime == null)
936                _ps.setNull(10, Types.BIGINT);
937            else
938                _ps.setLong(10, peop_fromtime);
939            
940            Long peop_thrutime = _value.getThruTime();
941            if(peop_thrutime == null)
942                _ps.setNull(11, Types.BIGINT);
943            else
944                _ps.setLong(11, peop_thrutime);
945            
946            _ps.setLong(12, _value.getPrimaryKey().getEntityId());
947            
948            _value.clearHasBeenModified();
949        }
950        
951        return _hasBeenModified;
952    }
953    
954    @Override
955    public void store(Session session, Person entity)
956            throws PersistenceDatabaseException {
957        PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
958        
959        try {
960            PersonValue _value = entity.getPersonValue();
961            
962            if(bindForStore(_ps, _value)) {
963                if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
964                    int _count = _ps.executeUpdate();
965                    
966                    if(_count != 1) {
967                        throw new PersistenceDatabaseUpdateException("update failed, _count = " + _count);
968                    }
969                } else {
970                     _ps.executeUpdate();
971                }
972                
973                session.getValueCache().put(_value);
974            }
975        } catch (SQLException se) {
976            throw new PersistenceDatabaseException(se);
977        }
978    }
979    
980    @Override
981    public void store(Session session, Collection<Person> entities)
982            throws PersistenceDatabaseException {
983        if(entities.size() > 0) {
984            PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
985            int _modifiedEntities = 0;
986            
987            try {
988                for(Person entity : entities) {
989                    if(bindForStore(_ps, entity.getPersonValue())) {
990                        _ps.addBatch();
991                        _modifiedEntities++;
992                    }
993                }
994                
995                if(_modifiedEntities != 0) {
996                    if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
997                        int[] _counts = _ps.executeBatch();
998                        
999                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
1000                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
1001                                throw new PersistenceDatabaseUpdateException("batch update failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
1002                            }
1003                        }
1004                    } else {
1005                         _ps.executeBatch();
1006                    }
1007                    
1008                    _ps.clearBatch();
1009                    
1010                    entities.forEach((entity) -> {
1011                        session.getValueCache().put(entity.getPersonValue());
1012                    });
1013                }
1014            } catch (SQLException se) {
1015                throw new PersistenceDatabaseException(se);
1016            }
1017        }
1018    }
1019    
1020    @Override
1021    public void store(Collection<Person> entities)
1022            throws PersistenceDatabaseException {
1023        store(ThreadSession.currentSession(), entities);
1024    }
1025    
1026    @Override
1027    public void remove(Session session, Person entity)
1028            throws PersistenceDatabaseException {
1029        remove(session, entity.getPrimaryKey());
1030    }
1031    
1032    @Override
1033    public void remove(Session session, PersonPK pk)
1034            throws PersistenceDatabaseException {
1035        PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
1036        long _entityId = pk.getEntityId();
1037        
1038        try {
1039            _ps.setLong(1, _entityId);
1040            
1041            if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
1042                int _count = _ps.executeUpdate();
1043                
1044                if(_count != 1) {
1045                    throw new PersistenceDatabaseUpdateException("remove failed, _count = " + _count);
1046                }
1047            } else {
1048                 _ps.executeUpdate();
1049            }
1050            
1051            session.getValueCache().remove(pk);
1052        } catch (SQLException se) {
1053            throw new PersistenceDatabaseException(se);
1054        }
1055        
1056        session.removed(pk, false);
1057    }
1058    
1059    @Override
1060    public void remove(Session session, Collection<PersonPK> pks)
1061            throws PersistenceDatabaseException {
1062        if(pks.size() > 0) {
1063            PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
1064            int _modifiedEntities = 0;
1065            
1066            try {
1067                for(PersonPK pk : pks) {
1068                    long _entityId = pk.getEntityId();
1069                    
1070                    _ps.setLong(1, _entityId);
1071                    
1072                    _ps.addBatch();
1073                    _modifiedEntities++;
1074                }
1075                
1076                if(_modifiedEntities != 0) {
1077                    if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
1078                        int[] _counts = _ps.executeBatch();
1079                        
1080                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
1081                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
1082                                throw new PersistenceDatabaseUpdateException("batch remove failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
1083                            }
1084                        }
1085                    } else {
1086                        _ps.executeBatch();
1087                    }
1088                    
1089                    _ps.clearBatch();
1090                    
1091                    pks.forEach((pk) -> {
1092                        session.getValueCache().remove(pk);
1093                    });
1094                }
1095            } catch (SQLException se) {
1096                throw new PersistenceDatabaseException(se);
1097            }
1098            
1099            pks.forEach((pk) -> {
1100                session.removed(pk, true);
1101            });
1102        }
1103    }
1104    
1105    @Override
1106    public void remove(Collection<PersonPK> pks)
1107            throws PersistenceDatabaseException {
1108        remove(ThreadSession.currentSession(), pks);
1109    }
1110    
1111    @Override
1112    public boolean validPK(Session session, PersonPK pk)
1113            throws PersistenceDatabaseException {
1114        boolean valid = false;
1115        PreparedStatement _ps = session.prepareStatement(SQL_VALID);
1116        ResultSet _rs = null;
1117        
1118        try {
1119            _ps.setLong(1, pk.getEntityId());
1120            
1121            _rs = _ps.executeQuery();
1122            if(_rs.next()) {
1123                long _count = _rs.getLong(1);
1124                if(_rs.wasNull())
1125                    _count = 0;
1126                
1127                if(_count == 1)
1128                    valid = true;
1129            }
1130        } catch (SQLException se) {
1131            throw new PersistenceDatabaseException(se);
1132        } finally {
1133            if(_rs != null) {
1134                try {
1135                    _rs.close();
1136                } catch (SQLException se) {
1137                    // do nothing
1138                }
1139            }
1140        }
1141        
1142        return valid;
1143    }
1144    
1145}