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