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