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