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