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