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