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