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