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