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