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