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