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