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