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