001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016// Generated File -- DO NOT EDIT BY HAND
017// --------------------------------------------------------------------------------
018
019/**
020 * PartyAliasFactory.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.PartyAliasTypePK;
027
028import com.echothree.model.data.party.server.entity.Party;
029import com.echothree.model.data.party.server.entity.PartyAliasType;
030
031import com.echothree.model.data.party.common.PartyAliasConstants;
032import com.echothree.model.data.party.common.pk.PartyAliasPK;
033import com.echothree.model.data.party.server.value.PartyAliasValue;
034import com.echothree.model.data.party.server.entity.PartyAlias;
035import com.echothree.util.common.exception.PersistenceDatabaseException;
036import com.echothree.util.common.exception.PersistenceDatabaseUpdateException;
037import com.echothree.util.common.exception.PersistenceNotNullException;
038import com.echothree.util.server.persistence.BaseFactory;
039import com.echothree.util.server.persistence.EntityIdGenerator;
040import com.echothree.util.server.persistence.EntityPermission;
041import com.echothree.util.server.persistence.PersistenceDebugFlags;
042import com.echothree.util.server.persistence.Session;
043import java.sql.PreparedStatement;
044import java.sql.ResultSet;
045import java.sql.SQLException;
046import java.sql.Types;
047import java.io.ByteArrayInputStream;
048import java.io.StringReader;
049import java.util.ArrayList;
050import java.util.Collection;
051import java.util.HashSet;
052import java.util.List;
053import java.util.Map;
054import java.util.Set;
055import javax.enterprise.context.ApplicationScoped;
056import javax.enterprise.inject.spi.CDI;
057import javax.inject.Inject;
058import org.jooq.ResultQuery;
059import org.slf4j.Logger;
060import org.slf4j.LoggerFactory;
061
062@ApplicationScoped
063public class PartyAliasFactory
064        implements BaseFactory<PartyAliasPK, PartyAlias> {
065    
066    @Inject
067    Session session;
068    
069    //private static final Logger log = LoggerFactory.getLogger(PartyAliasFactory.class);
070    
071    final private static String SQL_SELECT_READ_ONLY = "SELECT pal_partyaliasid, pal_par_partyid, pal_pat_partyaliastypeid, pal_alias, pal_fromtime, pal_thrutime FROM partyaliases WHERE pal_partyaliasid = ?";
072    final private static String SQL_SELECT_READ_WRITE = "SELECT pal_partyaliasid, pal_par_partyid, pal_pat_partyaliastypeid, pal_alias, pal_fromtime, pal_thrutime FROM partyaliases WHERE pal_partyaliasid = ? FOR UPDATE";
073    final private static String SQL_INSERT = "INSERT INTO partyaliases (pal_partyaliasid, pal_par_partyid, pal_pat_partyaliastypeid, pal_alias, pal_fromtime, pal_thrutime) VALUES (?, ?, ?, ?, ?, ?)";
074    final private static String SQL_UPDATE = "UPDATE partyaliases SET pal_par_partyid = ?, pal_pat_partyaliastypeid = ?, pal_alias = ?, pal_fromtime = ?, pal_thrutime = ? WHERE pal_partyaliasid = ?";
075    final private static String SQL_DELETE = "DELETE FROM partyaliases WHERE pal_partyaliasid = ?";
076    final private static String SQL_VALID = "SELECT COUNT(*) FROM partyaliases WHERE pal_partyaliasid = ?";
077    
078    final public static String PAL_PARTYALIASID = "pal_partyaliasid";
079    final public static String PAL_PAR_PARTYID = "pal_par_partyid";
080    final public static String PAL_PAT_PARTYALIASTYPEID = "pal_pat_partyaliastypeid";
081    final public static String PAL_ALIAS = "pal_alias";
082    final public static String PAL_FROMTIME = "pal_fromtime";
083    final public static String PAL_THRUTIME = "pal_thrutime";
084    
085    final private static EntityIdGenerator entityIdGenerator = new EntityIdGenerator(PartyAliasConstants.COMPONENT_VENDOR_NAME, PartyAliasConstants.ENTITY_TYPE_NAME);
086    
087    /** Creates a new instance of PartyAliasFactory */
088    protected PartyAliasFactory() {
089        super();
090    }
091    
092    public static PartyAliasFactory getInstance() {
093        return CDI.current().select(PartyAliasFactory.class).get();
094    }
095    
096    @Override
097    public String getComponentVendorName() {
098        return PartyAliasConstants.COMPONENT_VENDOR_NAME;
099    }
100    
101    @Override
102    public String getEntityTypeName() {
103        return PartyAliasConstants.ENTITY_TYPE_NAME;
104    }
105    
106    public PreparedStatement prepareStatement(String query) {
107        return session.prepareStatement(query);
108    }
109    
110    public PartyAliasPK getNextPK() {
111        return new PartyAliasPK(entityIdGenerator.getNextEntityId());
112    }
113    
114    public Set<PartyAliasPK> getPKsFromResultSetAsSet(ResultSet rs)
115            throws PersistenceDatabaseException {
116        Set<PartyAliasPK> _result = new HashSet<>();
117        
118        try {
119            while(rs.next()) {
120                _result.add(getPKFromResultSet(rs));
121            }
122        } catch (SQLException se) {
123            throw new PersistenceDatabaseException(se);
124        }
125        
126        return _result;
127    }
128    
129    public java.util.List<PartyAliasPK> getPKsFromResultSetAsList(ResultSet rs)
130            throws PersistenceDatabaseException {
131        java.util.List<PartyAliasPK> _result = new ArrayList<>();
132        
133        try {
134            while(rs.next()) {
135                _result.add(getPKFromResultSet(rs));
136            }
137        } catch (SQLException se) {
138            throw new PersistenceDatabaseException(se);
139        }
140        
141        return _result;
142    }
143    
144    public PartyAliasPK getPKFromResultSet(ResultSet rs)
145            throws PersistenceDatabaseException {
146        PartyAliasPK _result;
147        
148        try {
149            long pal_partyaliasid = rs.getLong(PAL_PARTYALIASID);
150            Long _entityId = rs.wasNull() ? null : pal_partyaliasid;
151            
152            _result = new PartyAliasPK(_entityId);
153        } catch (SQLException se) {
154            throw new PersistenceDatabaseException(se);
155        }
156        
157        return _result;
158    }
159    
160    public java.util.List<PartyAliasValue> getValuesFromPKs(Collection<PartyAliasPK> pks)
161            throws PersistenceDatabaseException {
162        java.util.List<PartyAliasValue> _values = new ArrayList<>(pks.size());
163        
164        for(PartyAliasPK _pk: pks) {
165            _values.add(getValueFromPK(_pk));
166        }
167        
168        return _values;
169    }
170    
171    public PartyAliasValue getValueFromPK(PartyAliasPK pk)
172            throws PersistenceDatabaseException {
173        PartyAliasValue _value;
174        
175        // See if we already have the entity in the session cache
176        PartyAlias _entity = (PartyAlias)session.getEntity(pk);
177        if(_entity == null)
178            _value = getEntityFromPK(EntityPermission.READ_ONLY, pk).getPartyAliasValue();
179        else
180            _value = _entity.getPartyAliasValue();
181        
182        return _value;
183    }
184    
185    public java.util.List<PartyAliasValue> getValuesFromResultSet(ResultSet rs)
186            throws PersistenceDatabaseException {
187        java.util.List<PartyAliasValue> _result = new ArrayList<>();
188        
189        try {
190            while(rs.next()) {
191                _result.add(getValueFromResultSet(rs));
192            }
193        } catch (SQLException se) {
194            throw new PersistenceDatabaseException(se);
195        }
196        
197        return _result;
198    }
199    
200    public PartyAliasValue getValueFromResultSet(ResultSet rs)
201            throws PersistenceDatabaseException {
202        PartyAliasValue _value;
203        
204        try {
205            Long pal_partyaliasid = rs.getLong(PAL_PARTYALIASID);
206            PartyAliasPK _pk = new PartyAliasPK(pal_partyaliasid);
207            
208            // See if we already have the entity in the session cache
209            PartyAlias _entity = (PartyAlias)session.getEntity(_pk);
210            
211            if(_entity == null) {
212                Long pal_par_partyid = rs.getLong(PAL_PAR_PARTYID);
213                if(rs.wasNull())
214                    pal_par_partyid = null;
215                
216                Long pal_pat_partyaliastypeid = rs.getLong(PAL_PAT_PARTYALIASTYPEID);
217                if(rs.wasNull())
218                    pal_pat_partyaliastypeid = null;
219                
220                String pal_alias = rs.getString(PAL_ALIAS);
221                if(rs.wasNull())
222                    pal_alias = null;
223                
224                Long pal_fromtime = rs.getLong(PAL_FROMTIME);
225                if(rs.wasNull())
226                    pal_fromtime = null;
227                
228                Long pal_thrutime = rs.getLong(PAL_THRUTIME);
229                if(rs.wasNull())
230                    pal_thrutime = null;
231                
232                _value = new PartyAliasValue(_pk, new PartyPK(pal_par_partyid), new PartyAliasTypePK(pal_pat_partyaliastypeid), pal_alias, pal_fromtime, pal_thrutime);
233            } else
234                _value = _entity.getPartyAliasValue();
235        } catch (SQLException se) {
236            throw new PersistenceDatabaseException(se);
237        }
238        
239        return _value;
240    }
241    
242    public java.util.List<PartyAlias> getEntitiesFromPKs(EntityPermission entityPermission, Collection<PartyAliasPK> pks)
243            throws PersistenceDatabaseException {
244        java.util.List<PartyAlias> _entities = new ArrayList<>(pks.size());
245        
246        for(PartyAliasPK _pk: pks) {
247            _entities.add(getEntityFromPK(entityPermission, _pk));
248        }
249        
250        return _entities;
251    }
252    
253    public PartyAlias getEntityFromValue(EntityPermission entityPermission, PartyAliasValue value) {
254        return getEntityFromPK(entityPermission, value.getPrimaryKey());
255    }
256    
257    public PartyAlias getEntityFromCache(PartyAliasPK pk) {
258        PartyAliasValue _value = (PartyAliasValue)session.getValueCache().get(pk);
259    
260        return _value == null ? null : new PartyAlias(_value, EntityPermission.READ_ONLY);
261    }
262    
263    public PartyAlias getEntityFromPK(EntityPermission entityPermission, PartyAliasPK pk)
264            throws PersistenceDatabaseException {
265        PartyAlias _entity;
266        
267        // See if we already have the entity in the session cache
268        _entity = (PartyAlias)session.getEntity(pk);
269        if(_entity != null) {
270            // If the requested permission is READ_WRITE, and the cached permission is
271            // READ_ONLY, then pretend that the cached object wasn't found, and create
272            // a new entity that is READ_WRITE.
273            if(entityPermission.equals(EntityPermission.READ_WRITE)) {
274                if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
275                    _entity = null;
276            }
277        }
278        
279        if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
280            _entity = getEntityFromCache(pk);
281        }
282        
283        if(_entity == null) {
284            PreparedStatement _ps = session.prepareStatement(entityPermission.equals(EntityPermission.READ_ONLY)? SQL_SELECT_READ_ONLY: SQL_SELECT_READ_WRITE);
285            long _entityId = pk.getEntityId();
286            ResultSet _rs = null;
287            
288            try {
289                _ps.setLong(1, _entityId);
290                _rs = _ps.executeQuery();
291                if(_rs.next()) {
292                    _entity = getEntityFromResultSet(entityPermission, _rs);
293                }
294            } catch (SQLException se) {
295                throw new PersistenceDatabaseException(se);
296            } finally {
297                if(_rs != null) {
298                    try {
299                        _rs.close();
300                    } catch (SQLException se) {
301                        // do nothing
302                    }
303                }
304            }
305        }
306        
307        return _entity;
308    }
309    
310    public Set<PartyAliasPK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params)
311            throws PersistenceDatabaseException {
312        Set<PartyAliasPK> _pks;
313        ResultSet _rs = null;
314        
315        try {
316            if(params.length != 0) {
317                Session.setQueryParams(ps, params);
318            }
319            
320            _rs = ps.executeQuery();
321            _pks = getPKsFromResultSetAsSet(_rs);
322            _rs.close();
323        } catch (SQLException se) {
324            throw new PersistenceDatabaseException(se);
325        } finally {
326            if(_rs != null) {
327                try {
328                    _rs.close();
329                } catch (SQLException se) {
330                    // do nothing
331                }
332            }
333        }
334        
335        return _pks;
336    }
337    
338    public java.util.List<PartyAliasPK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params)
339            throws PersistenceDatabaseException {
340        java.util.List<PartyAliasPK> _pks;
341        ResultSet _rs = null;
342        
343        try {
344            if(params.length != 0) {
345                Session.setQueryParams(ps, params);
346            }
347            
348            _rs = ps.executeQuery();
349            _pks = getPKsFromResultSetAsList(_rs);
350            _rs.close();
351        } catch (SQLException se) {
352            throw new PersistenceDatabaseException(se);
353        } finally {
354            if(_rs != null) {
355                try {
356                    _rs.close();
357                } catch (SQLException se) {
358                    // do nothing
359                }
360            }
361        }
362        
363        return _pks;
364    }
365    
366    public PartyAliasPK getPKFromQuery(PreparedStatement ps, final Object... params)
367            throws PersistenceDatabaseException {
368        PartyAliasPK _pk = null;
369        ResultSet _rs = null;
370        
371        try {
372            if(params.length != 0) {
373                Session.setQueryParams(ps, params);
374            }
375            
376            _rs = ps.executeQuery();
377            if(_rs.next()) {
378                _pk = getPKFromResultSet(_rs);
379            }
380            _rs.close();
381        } catch (SQLException se) {
382            throw new PersistenceDatabaseException(se);
383        } finally {
384            if(_rs != null) {
385                try {
386                    _rs.close();
387                } catch (SQLException se) {
388                    // do nothing
389                }
390            }
391        }
392        
393        return _pk;
394    }
395    
396    public java.util.List<PartyAlias> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
397            throws PersistenceDatabaseException {
398        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
399        
400        return getEntitiesFromQuery(entityPermission, ps, params);
401    }
402    
403    public java.util.List<PartyAlias> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
404            throws PersistenceDatabaseException {
405        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
406        
407        return getEntitiesFromQuery(entityPermission, ps);
408    }
409    
410    public java.util.List<PartyAlias> getEntitiesFromQuery(EntityPermission entityPermission, ResultQuery<?> query)
411            throws PersistenceDatabaseException {
412        PreparedStatement ps = session.prepareStatement(query.getSQL());
413        
414        return getEntitiesFromQuery(entityPermission, ps, query.getBindValues().toArray());
415    }
416    
417    public java.util.List<PartyAlias> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
418            throws PersistenceDatabaseException {
419        java.util.List<PartyAlias> _entities;
420        ResultSet _rs = null;
421        
422        try {
423            if(params.length != 0) {
424                Session.setQueryParams(ps, params);
425            }
426            
427            _rs = ps.executeQuery();
428            _entities = getEntitiesFromResultSet(entityPermission, _rs);
429            _rs.close();
430        } catch (SQLException se) {
431            throw new PersistenceDatabaseException(se);
432        } finally {
433            if(_rs != null) {
434                try {
435                    _rs.close();
436                } catch (SQLException se) {
437                    // do nothing
438                }
439            }
440        }
441        
442        return _entities;
443    }
444    
445    public PartyAlias getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
446            throws PersistenceDatabaseException {
447        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
448        
449        return getEntityFromQuery(entityPermission, ps, params);
450    }
451    
452    public PartyAlias getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
453            throws PersistenceDatabaseException {
454        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
455        
456        return getEntityFromQuery(entityPermission, ps);
457    }
458    
459    public PartyAlias getEntityFromQuery(EntityPermission entityPermission, ResultQuery<?> query)
460            throws PersistenceDatabaseException {
461        PreparedStatement ps = session.prepareStatement(query.getSQL());
462        
463        return getEntityFromQuery(entityPermission, ps, query.getBindValues().toArray());
464    }
465    
466    public PartyAlias getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
467            throws PersistenceDatabaseException {
468        PartyAlias _entity = null;
469        ResultSet _rs = null;
470        
471        try {
472            if(params.length != 0) {
473                Session.setQueryParams(ps, params);
474            }
475            
476            _rs = ps.executeQuery();
477            if(_rs.next()) {
478                _entity = getEntityFromResultSet(entityPermission, _rs);
479            }
480            _rs.close();
481        } catch (SQLException se) {
482            throw new PersistenceDatabaseException(se);
483        } finally {
484            if(_rs != null) {
485                try {
486                    _rs.close();
487                } catch (SQLException se) {
488                    // do nothing
489                }
490            }
491        }
492        
493        return _entity;
494    }
495    
496    public java.util.List<PartyAlias> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs)
497            throws PersistenceDatabaseException {
498        java.util.List<PartyAlias> _result = new ArrayList<>();
499        
500        try {
501            while(rs.next()) {
502                _result.add(getEntityFromResultSet(entityPermission, rs));
503            }
504        } catch (SQLException se) {
505            throw new PersistenceDatabaseException(se);
506        }
507        
508        return _result;
509    }
510    
511    public PartyAlias getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs)
512            throws PersistenceDatabaseException {
513        PartyAlias _entity;
514        
515        try {
516            Long pal_partyaliasid = rs.getLong(PAL_PARTYALIASID);
517            PartyAliasPK _pk = new PartyAliasPK(pal_partyaliasid);
518            
519            // See if we already have the entity in the session cache
520            _entity = (PartyAlias)session.getEntity(_pk);
521            if(_entity != null) {
522                // If the requested permission is READ_WRITE, and the cached permission is
523                // READ_ONLY, then pretend that the cached object wasn't found, and create
524                // a new entity that is READ_WRITE.
525                if(entityPermission.equals(EntityPermission.READ_WRITE)) {
526                    if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
527                        _entity = null;
528                }
529            }
530            boolean foundInSessionCache = _entity != null;
531            
532            if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
533                _entity = getEntityFromCache(_pk);
534            }
535            
536            if(_entity == null) {
537                Long pal_par_partyid = rs.getLong(PAL_PAR_PARTYID);
538                if(rs.wasNull())
539                    pal_par_partyid = null;
540                
541                Long pal_pat_partyaliastypeid = rs.getLong(PAL_PAT_PARTYALIASTYPEID);
542                if(rs.wasNull())
543                    pal_pat_partyaliastypeid = null;
544                
545                String pal_alias = rs.getString(PAL_ALIAS);
546                if(rs.wasNull())
547                    pal_alias = null;
548                
549                Long pal_fromtime = rs.getLong(PAL_FROMTIME);
550                if(rs.wasNull())
551                    pal_fromtime = null;
552                
553                Long pal_thrutime = rs.getLong(PAL_THRUTIME);
554                if(rs.wasNull())
555                    pal_thrutime = null;
556                
557                PartyAliasValue _value = new PartyAliasValue(_pk, pal_par_partyid == null? null: new PartyPK(pal_par_partyid), pal_pat_partyaliastypeid == null? null: new PartyAliasTypePK(pal_pat_partyaliastypeid), pal_alias, pal_fromtime, pal_thrutime);
558                _entity = new PartyAlias(_value, entityPermission);
559            }
560            
561            if(!foundInSessionCache) {
562                if(entityPermission.equals(EntityPermission.READ_ONLY)) {
563                    session.putReadOnlyEntity(_pk, _entity);
564                    session.getValueCache().put(_entity.getPartyAliasValue());
565                } else {
566                    session.putReadWriteEntity(_pk, _entity);
567                }
568            }
569        } catch (SQLException se) {
570            throw new PersistenceDatabaseException(se);
571        }
572        
573        return _entity;
574    }
575    
576    public PartyAlias create(Party party, PartyAliasType partyAliasType, String alias, Long fromTime, Long thruTime)
577            throws PersistenceDatabaseException, PersistenceNotNullException {
578        return create(party == null ? null : party.getPrimaryKey(), partyAliasType == null ? null : partyAliasType.getPrimaryKey(), alias, fromTime, thruTime);
579    }
580    
581    private void bindForCreate(PreparedStatement _ps, PartyAliasValue _value)
582            throws SQLException {
583        _ps.setLong(1, _value.getEntityId());
584        
585        PartyPK pal_par_partyid = _value.getPartyPK();
586        if(pal_par_partyid == null)
587            _ps.setNull(2, Types.BIGINT);
588        else
589            _ps.setLong(2, pal_par_partyid.getEntityId());
590            
591        PartyAliasTypePK pal_pat_partyaliastypeid = _value.getPartyAliasTypePK();
592        if(pal_pat_partyaliastypeid == null)
593            _ps.setNull(3, Types.BIGINT);
594        else
595            _ps.setLong(3, pal_pat_partyaliastypeid.getEntityId());
596            
597        String pal_alias = _value.getAlias();
598        if(pal_alias == null)
599            _ps.setNull(4, Types.VARCHAR);
600        else
601            _ps.setString(4, pal_alias);
602            
603        Long pal_fromtime = _value.getFromTime();
604        if(pal_fromtime == null)
605            _ps.setNull(5, Types.BIGINT);
606        else
607            _ps.setLong(5, pal_fromtime);
608            
609        Long pal_thrutime = _value.getThruTime();
610        if(pal_thrutime == null)
611            _ps.setNull(6, Types.BIGINT);
612        else
613            _ps.setLong(6, pal_thrutime);
614            
615    }
616    
617    public PartyAlias create(PartyPK partyPK, PartyAliasTypePK partyAliasTypePK, String alias, Long fromTime, Long thruTime)
618            throws PersistenceDatabaseException, PersistenceNotNullException {
619        PartyAliasPK _pk = getNextPK();
620        PartyAliasValue _value = new PartyAliasValue(_pk, partyPK, partyAliasTypePK, alias, fromTime, thruTime);
621        
622        PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
623        
624        try {
625            bindForCreate(_ps, _value);
626            
627            if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
628                int _count = _ps.executeUpdate();
629                
630                if(_count != 1) {
631                    throw new PersistenceDatabaseUpdateException("insert failed, _count = " + _count);
632                }
633            } else {
634                 _ps.executeUpdate();
635            }
636            
637            session.getValueCache().put(_value);
638        } catch (SQLException se) {
639            throw new PersistenceDatabaseException(se);
640        }
641        
642        PartyAlias _entity = new PartyAlias(_value, EntityPermission.READ_ONLY);
643        session.putReadOnlyEntity(_pk, _entity);
644        
645        return _entity;
646    }
647    
648    public void create(Collection<PartyAliasValue> _values)
649            throws PersistenceDatabaseException, PersistenceNotNullException {
650        int _size = _values.size();
651        
652        if(_size > 0) {
653            PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
654            List<PartyAliasValue> _cacheValues = new ArrayList<>(_size);
655            
656            try {
657                for(PartyAliasValue _value : _values) {
658                    _value.setEntityId(entityIdGenerator.getNextEntityId());
659                    bindForCreate(_ps, _value);
660                    
661                    _ps.addBatch();
662                    
663                    _cacheValues.add(_value);
664                }
665                
666                if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
667                    int[] _counts = _ps.executeBatch();
668                    
669                    for(int _countOffset = 0 ; _countOffset < _size ; _countOffset++) {
670                        if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
671                            throw new PersistenceDatabaseUpdateException("batch insert failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
672                        }
673                    }
674                } else {
675                     _ps.executeBatch();
676                }
677                
678                _ps.clearBatch();
679            } catch (SQLException se) {
680                throw new PersistenceDatabaseException(se);
681            }
682            
683            _cacheValues.forEach((_cacheValue) -> {
684                PartyAlias _cacheEntity = new PartyAlias(_cacheValue, EntityPermission.READ_ONLY);
685                
686                session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity);
687            });
688        }
689    }
690    
691    private boolean bindForStore(PreparedStatement _ps, PartyAliasValue _value)
692            throws SQLException {
693        boolean _hasBeenModified = _value.hasBeenModified();
694        
695        if(_hasBeenModified) {
696            PartyPK pal_par_partyid = _value.getPartyPK();
697            if(pal_par_partyid == null)
698                _ps.setNull(1, Types.BIGINT);
699            else
700                _ps.setLong(1, pal_par_partyid.getEntityId());
701            
702            PartyAliasTypePK pal_pat_partyaliastypeid = _value.getPartyAliasTypePK();
703            if(pal_pat_partyaliastypeid == null)
704                _ps.setNull(2, Types.BIGINT);
705            else
706                _ps.setLong(2, pal_pat_partyaliastypeid.getEntityId());
707            
708            String pal_alias = _value.getAlias();
709            if(pal_alias == null)
710                _ps.setNull(3, Types.VARCHAR);
711            else
712                _ps.setString(3, pal_alias);
713            
714            Long pal_fromtime = _value.getFromTime();
715            if(pal_fromtime == null)
716                _ps.setNull(4, Types.BIGINT);
717            else
718                _ps.setLong(4, pal_fromtime);
719            
720            Long pal_thrutime = _value.getThruTime();
721            if(pal_thrutime == null)
722                _ps.setNull(5, Types.BIGINT);
723            else
724                _ps.setLong(5, pal_thrutime);
725            
726            _ps.setLong(6, _value.getPrimaryKey().getEntityId());
727            
728            _value.clearHasBeenModified();
729        }
730        
731        return _hasBeenModified;
732    }
733    
734    @Override
735    public void store(PartyAlias entity)
736            throws PersistenceDatabaseException {
737        PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
738        
739        try {
740            PartyAliasValue _value = entity.getPartyAliasValue();
741            
742            if(bindForStore(_ps, _value)) {
743                if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
744                    int _count = _ps.executeUpdate();
745                    
746                    if(_count != 1) {
747                        throw new PersistenceDatabaseUpdateException("update failed, _count = " + _count);
748                    }
749                } else {
750                     _ps.executeUpdate();
751                }
752                
753                session.getValueCache().put(_value);
754            }
755        } catch (SQLException se) {
756            throw new PersistenceDatabaseException(se);
757        }
758    }
759    
760    @Override
761    public void store(Collection<PartyAlias> entities)
762            throws PersistenceDatabaseException {
763        if(entities.size() > 0) {
764            PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
765            int _modifiedEntities = 0;
766            
767            try {
768                for(PartyAlias entity : entities) {
769                    if(bindForStore(_ps, entity.getPartyAliasValue())) {
770                        _ps.addBatch();
771                        _modifiedEntities++;
772                    }
773                }
774                
775                if(_modifiedEntities != 0) {
776                    if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
777                        int[] _counts = _ps.executeBatch();
778                        
779                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
780                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
781                                throw new PersistenceDatabaseUpdateException("batch update failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
782                            }
783                        }
784                    } else {
785                         _ps.executeBatch();
786                    }
787                    
788                    _ps.clearBatch();
789                    
790                    entities.forEach((entity) -> {
791                        session.getValueCache().put(entity.getPartyAliasValue());
792                    });
793                }
794            } catch (SQLException se) {
795                throw new PersistenceDatabaseException(se);
796            }
797        }
798    }
799    
800    @Override
801    public void remove(PartyAlias entity)
802            throws PersistenceDatabaseException {
803        remove(entity.getPrimaryKey());
804    }
805    
806    @Override
807    public void remove(PartyAliasPK pk)
808            throws PersistenceDatabaseException {
809        PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
810        long _entityId = pk.getEntityId();
811        
812        try {
813            _ps.setLong(1, _entityId);
814            
815            if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
816                int _count = _ps.executeUpdate();
817                
818                if(_count != 1) {
819                    throw new PersistenceDatabaseUpdateException("remove failed, _count = " + _count);
820                }
821            } else {
822                 _ps.executeUpdate();
823            }
824            
825            session.getValueCache().remove(pk);
826        } catch (SQLException se) {
827            throw new PersistenceDatabaseException(se);
828        }
829        
830        session.removed(pk, false);
831    }
832    
833    @Override
834    public void remove(Collection<PartyAliasPK> pks)
835            throws PersistenceDatabaseException {
836        if(pks.size() > 0) {
837            PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
838            int _modifiedEntities = 0;
839            
840            try {
841                for(PartyAliasPK pk : pks) {
842                    long _entityId = pk.getEntityId();
843                    
844                    _ps.setLong(1, _entityId);
845                    
846                    _ps.addBatch();
847                    _modifiedEntities++;
848                }
849                
850                if(_modifiedEntities != 0) {
851                    if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
852                        int[] _counts = _ps.executeBatch();
853                        
854                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
855                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
856                                throw new PersistenceDatabaseUpdateException("batch remove failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
857                            }
858                        }
859                    } else {
860                        _ps.executeBatch();
861                    }
862                    
863                    _ps.clearBatch();
864                    
865                    pks.forEach((pk) -> {
866                        session.getValueCache().remove(pk);
867                    });
868                }
869            } catch (SQLException se) {
870                throw new PersistenceDatabaseException(se);
871            }
872            
873            pks.forEach((pk) -> {
874                session.removed(pk, true);
875            });
876        }
877    }
878    
879    @Override
880    public boolean validPK(PartyAliasPK pk)
881            throws PersistenceDatabaseException {
882        boolean valid = false;
883        PreparedStatement _ps = session.prepareStatement(SQL_VALID);
884        ResultSet _rs = null;
885        
886        try {
887            _ps.setLong(1, pk.getEntityId());
888            
889            _rs = _ps.executeQuery();
890            if(_rs.next()) {
891                long _count = _rs.getLong(1);
892                if(_rs.wasNull())
893                    _count = 0;
894                
895                if(_count == 1)
896                    valid = true;
897            }
898        } catch (SQLException se) {
899            throw new PersistenceDatabaseException(se);
900        } finally {
901            if(_rs != null) {
902                try {
903                    _rs.close();
904                } catch (SQLException se) {
905                    // do nothing
906                }
907            }
908        }
909        
910        return valid;
911    }
912    
913}