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