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