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