Source Code Cross Referenced for HibernateUserManagerImpl.java in  » Blogger-System » apache-roller-3.1 » org » apache » roller » business » hibernate » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Blogger System » apache roller 3.1 » org.apache.roller.business.hibernate 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * Licensed to the Apache Software Foundation (ASF) under one or more
0003:         *  contributor license agreements.  The ASF licenses this file to You
0004:         * under the Apache License, Version 2.0 (the "License"); you may not
0005:         * use this file except in compliance with the License.
0006:         * You may obtain a copy of the License at
0007:         *
0008:         *     http://www.apache.org/licenses/LICENSE-2.0
0009:         *
0010:         * Unless required by applicable law or agreed to in writing, software
0011:         * distributed under the License is distributed on an "AS IS" BASIS,
0012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013:         * See the License for the specific language governing permissions and
0014:         * limitations under the License.  For additional information regarding
0015:         * copyright in this work, please see the NOTICE file in the top level
0016:         * directory of this distribution.
0017:         */
0018:        /* Created on Jun 16, 2004 */
0019:        package org.apache.roller.business.hibernate;
0020:
0021:        import java.util.ArrayList;
0022:        import java.util.Date;
0023:        import java.util.HashMap;
0024:        import java.util.Hashtable;
0025:        import java.util.Iterator;
0026:        import java.util.List;
0027:        import java.util.Map;
0028:        import java.util.TreeMap;
0029:        import org.apache.roller.pojos.StatCount;
0030:        import org.hibernate.Criteria;
0031:        import org.hibernate.HibernateException;
0032:        import org.hibernate.Session;
0033:        import org.hibernate.criterion.Expression;
0034:        import org.hibernate.criterion.MatchMode;
0035:        import org.hibernate.criterion.Order;
0036:        import org.apache.commons.logging.Log;
0037:        import org.apache.commons.logging.LogFactory;
0038:        import org.hibernate.criterion.SimpleExpression;
0039:        import org.apache.roller.RollerException;
0040:        import org.apache.roller.config.RollerConfig;
0041:        import org.apache.roller.business.pings.AutoPingManager;
0042:        import org.apache.roller.business.BookmarkManager;
0043:        import org.apache.roller.business.pings.PingTargetManager;
0044:        import org.apache.roller.business.RollerFactory;
0045:        import org.apache.roller.business.UserManager;
0046:        import org.apache.roller.business.WeblogManager;
0047:        import org.apache.roller.pojos.AutoPingData;
0048:        import org.apache.roller.pojos.BookmarkData;
0049:        import org.apache.roller.pojos.FolderData;
0050:        import org.apache.roller.pojos.TagStat;
0051:        import org.apache.roller.pojos.WeblogEntryTagData;
0052:        import org.apache.roller.pojos.WeblogTemplate;
0053:        import org.apache.roller.pojos.PermissionsData;
0054:        import org.apache.roller.pojos.PingQueueEntryData;
0055:        import org.apache.roller.pojos.PingTargetData;
0056:        import org.apache.roller.pojos.RefererData;
0057:        import org.apache.roller.pojos.UserData;
0058:        import org.apache.roller.pojos.WeblogCategoryData;
0059:        import org.apache.roller.pojos.WeblogEntryData;
0060:        import org.apache.roller.pojos.WebsiteData;
0061:        import org.hibernate.Query;
0062:
0063:        /**
0064:         * Hibernate implementation of the UserManager.
0065:         */
0066:        public class HibernateUserManagerImpl implements  UserManager {
0067:
0068:            static final long serialVersionUID = -5128460637997081121L;
0069:            private static Log log = LogFactory
0070:                    .getLog(HibernateUserManagerImpl.class);
0071:            private HibernatePersistenceStrategy strategy = null;
0072:
0073:            // cached mapping of weblogHandles -> weblogIds
0074:            private Map weblogHandleToIdMap = new Hashtable();
0075:
0076:            // cached mapping of userNames -> userIds
0077:            private Map userNameToIdMap = new Hashtable();
0078:
0079:            public HibernateUserManagerImpl(HibernatePersistenceStrategy strat) {
0080:                log.debug("Instantiating Hibernate User Manager");
0081:
0082:                this .strategy = strat;
0083:            }
0084:
0085:            /**
0086:             * Update existing website.
0087:             */
0088:            public void saveWebsite(WebsiteData website) throws RollerException {
0089:
0090:                website.setLastModified(new java.util.Date());
0091:                strategy.store(website);
0092:            }
0093:
0094:            public void removeWebsite(WebsiteData weblog)
0095:                    throws RollerException {
0096:
0097:                // remove contents first, then remove website
0098:                this .removeWebsiteContents(weblog);
0099:                this .strategy.remove(weblog);
0100:
0101:                // remove entry from cache mapping
0102:                this .weblogHandleToIdMap.remove(weblog.getHandle());
0103:            }
0104:
0105:            /**
0106:             * convenience method for removing contents of a weblog.
0107:             * TODO BACKEND: use manager methods instead of queries here
0108:             */
0109:            private void removeWebsiteContents(WebsiteData website)
0110:                    throws HibernateException, RollerException {
0111:
0112:                Session session = this .strategy.getSession();
0113:
0114:                BookmarkManager bmgr = RollerFactory.getRoller()
0115:                        .getBookmarkManager();
0116:                WeblogManager wmgr = RollerFactory.getRoller()
0117:                        .getWeblogManager();
0118:
0119:                // remove tags
0120:                Criteria tagQuery = session.createCriteria(
0121:                        WeblogEntryTagData.class).add(
0122:                        Expression.eq("weblog.id", website.getId()));
0123:                for (Iterator iter = tagQuery.list().iterator(); iter.hasNext();) {
0124:                    WeblogEntryTagData tagData = (WeblogEntryTagData) iter
0125:                            .next();
0126:                    this .strategy.remove(tagData);
0127:                }
0128:
0129:                // remove site tag aggregates
0130:                List tags = wmgr.getTags(website, null, null, -1);
0131:                for (Iterator iter = tags.iterator(); iter.hasNext();) {
0132:                    TagStat stat = (TagStat) iter.next();
0133:                    Query query = session
0134:                            .createQuery("update WeblogEntryTagAggregateData set total = total - ? where name = ? and weblog is null");
0135:                    query.setParameter(0, new Integer(stat.getCount()));
0136:                    query.setParameter(1, stat.getName());
0137:                    query.executeUpdate();
0138:                }
0139:
0140:                // delete all weblog tag aggregates
0141:                session
0142:                        .createQuery(
0143:                                "delete from WeblogEntryTagAggregateData where weblog = ?")
0144:                        .setParameter(0, website).executeUpdate();
0145:
0146:                // delete all bad counts
0147:                session
0148:                        .createQuery(
0149:                                "delete from WeblogEntryTagAggregateData where total <= 0")
0150:                        .executeUpdate();
0151:
0152:                // Remove the website's ping queue entries
0153:                Criteria criteria = session
0154:                        .createCriteria(PingQueueEntryData.class);
0155:                criteria.add(Expression.eq("website", website));
0156:                List queueEntries = criteria.list();
0157:
0158:                // Remove the website's auto ping configurations
0159:                AutoPingManager autoPingMgr = RollerFactory.getRoller()
0160:                        .getAutopingManager();
0161:                List autopings = autoPingMgr.getAutoPingsByWebsite(website);
0162:                Iterator it = autopings.iterator();
0163:                while (it.hasNext()) {
0164:                    this .strategy.remove((AutoPingData) it.next());
0165:                }
0166:
0167:                // Remove the website's custom ping targets
0168:                PingTargetManager pingTargetMgr = RollerFactory.getRoller()
0169:                        .getPingTargetManager();
0170:                List pingtargets = pingTargetMgr.getCustomPingTargets(website);
0171:                it = pingtargets.iterator();
0172:                while (it.hasNext()) {
0173:                    this .strategy.remove((PingTargetData) it.next());
0174:                }
0175:
0176:                // remove entries
0177:                Criteria entryQuery = session
0178:                        .createCriteria(WeblogEntryData.class);
0179:                entryQuery.add(Expression.eq("website", website));
0180:                List entries = entryQuery.list();
0181:                for (Iterator iter = entries.iterator(); iter.hasNext();) {
0182:                    WeblogEntryData entry = (WeblogEntryData) iter.next();
0183:
0184:                    this .strategy.remove(entry);
0185:                }
0186:
0187:                // remove associated referers
0188:                Criteria refererQuery = session
0189:                        .createCriteria(RefererData.class);
0190:                refererQuery.add(Expression.eq("website", website));
0191:                List referers = refererQuery.list();
0192:                for (Iterator iter = referers.iterator(); iter.hasNext();) {
0193:                    RefererData referer = (RefererData) iter.next();
0194:                    this .strategy.remove(referer);
0195:                }
0196:
0197:                // remove associated pages
0198:                Criteria pageQuery = session
0199:                        .createCriteria(WeblogTemplate.class);
0200:                pageQuery.add(Expression.eq("website", website));
0201:                List pages = pageQuery.list();
0202:                for (Iterator iter = pages.iterator(); iter.hasNext();) {
0203:                    WeblogTemplate page = (WeblogTemplate) iter.next();
0204:                    this .strategy.remove(page);
0205:                }
0206:
0207:                // remove folders (including bookmarks)
0208:                FolderData rootFolder = bmgr.getRootFolder(website);
0209:                if (null != rootFolder) {
0210:                    this .strategy.remove(rootFolder);
0211:
0212:                    // Still cannot get all Bookmarks cleared!
0213:                    Iterator allFolders = bmgr.getAllFolders(website)
0214:                            .iterator();
0215:                    while (allFolders.hasNext()) {
0216:                        FolderData aFolder = (FolderData) allFolders.next();
0217:                        bmgr.removeFolderContents(aFolder);
0218:                        this .strategy.remove(aFolder);
0219:                    }
0220:                }
0221:
0222:                // remove categories
0223:                WeblogCategoryData rootCat = wmgr
0224:                        .getRootWeblogCategory(website);
0225:                if (null != rootCat) {
0226:                    this .strategy.remove(rootCat);
0227:                }
0228:
0229:            }
0230:
0231:            public void saveUser(UserData data) throws RollerException {
0232:                this .strategy.store(data);
0233:            }
0234:
0235:            public void removeUser(UserData user) throws RollerException {
0236:                this .strategy.remove(user);
0237:
0238:                // remove entry from cache mapping
0239:                this .userNameToIdMap.remove(user.getUserName());
0240:            }
0241:
0242:            public void savePermissions(PermissionsData perms)
0243:                    throws RollerException {
0244:                this .strategy.store(perms);
0245:            }
0246:
0247:            public void removePermissions(PermissionsData perms)
0248:                    throws RollerException {
0249:                this .strategy.remove(perms);
0250:            }
0251:
0252:            /**
0253:             * @see org.apache.roller.model.UserManager#storePage(org.apache.roller.pojos.WeblogTemplate)
0254:             */
0255:            public void savePage(WeblogTemplate page) throws RollerException {
0256:                this .strategy.store(page);
0257:
0258:                // update weblog last modified date.  date updated by saveWebsite()
0259:                RollerFactory.getRoller().getUserManager().saveWebsite(
0260:                        page.getWebsite());
0261:            }
0262:
0263:            public void removePage(WeblogTemplate page) throws RollerException {
0264:                this .strategy.remove(page);
0265:            }
0266:
0267:            public void addUser(UserData newUser) throws RollerException {
0268:
0269:                if (newUser == null)
0270:                    throw new RollerException("cannot add null user");
0271:
0272:                // TODO BACKEND: we must do this in a better fashion, like getUserCnt()?
0273:                boolean adminUser = false;
0274:                List existingUsers = this .getUsers(0, 1);
0275:                if (existingUsers.size() == 0) {
0276:                    // Make first user an admin
0277:                    adminUser = true;
0278:                }
0279:
0280:                if (getUserByUserName(newUser.getUserName()) != null
0281:                        || getUserByUserName(newUser.getUserName()
0282:                                .toLowerCase()) != null) {
0283:                    throw new RollerException("error.add.user.userNameInUse");
0284:                }
0285:
0286:                newUser.grantRole("editor");
0287:                if (adminUser) {
0288:                    newUser.grantRole("admin");
0289:                }
0290:
0291:                this .strategy.store(newUser);
0292:            }
0293:
0294:            public void addWebsite(WebsiteData newWeblog)
0295:                    throws RollerException {
0296:
0297:                this .strategy.store(newWeblog);
0298:                this .addWeblogContents(newWeblog);
0299:            }
0300:
0301:            private void addWeblogContents(WebsiteData newWeblog)
0302:                    throws RollerException {
0303:
0304:                UserManager umgr = RollerFactory.getRoller().getUserManager();
0305:                WeblogManager wmgr = RollerFactory.getRoller()
0306:                        .getWeblogManager();
0307:
0308:                // grant weblog creator ADMIN permissions
0309:                PermissionsData perms = new PermissionsData();
0310:                perms.setUser(newWeblog.getCreator());
0311:                perms.setWebsite(newWeblog);
0312:                perms.setPending(false);
0313:                perms.setPermissionMask(PermissionsData.ADMIN);
0314:                this .strategy.store(perms);
0315:
0316:                // add default categories
0317:                WeblogCategoryData rootCat = new WeblogCategoryData(null, // id
0318:                        newWeblog, // newWeblog
0319:                        null, // parent
0320:                        "root", // name
0321:                        "root", // description
0322:                        null); // image
0323:                this .strategy.store(rootCat);
0324:
0325:                String cats = RollerConfig.getProperty("newuser.categories");
0326:                WeblogCategoryData firstCat = rootCat;
0327:                if (cats != null) {
0328:                    String[] splitcats = cats.split(",");
0329:                    for (int i = 0; i < splitcats.length; i++) {
0330:                        WeblogCategoryData c = new WeblogCategoryData(null, // id
0331:                                newWeblog, // newWeblog
0332:                                rootCat, // parent
0333:                                splitcats[i], // name
0334:                                splitcats[i], // description
0335:                                null); // image
0336:                        if (i == 0)
0337:                            firstCat = c;
0338:                        this .strategy.store(c);
0339:                    }
0340:                }
0341:                // Use first category as default for Blogger API
0342:                newWeblog.setBloggerCategory(firstCat);
0343:
0344:                // But default category for weblog itself should be  root
0345:                newWeblog.setDefaultCategory(rootCat);
0346:
0347:                this .strategy.store(newWeblog);
0348:
0349:                // add default bookmarks
0350:                FolderData root = new FolderData(null, "root", "root",
0351:                        newWeblog);
0352:                this .strategy.store(root);
0353:
0354:                Integer zero = new Integer(0);
0355:                String blogroll = RollerConfig.getProperty("newuser.blogroll");
0356:                if (blogroll != null) {
0357:                    String[] splitroll = blogroll.split(",");
0358:                    for (int i = 0; i < splitroll.length; i++) {
0359:                        String[] rollitems = splitroll[i].split("\\|");
0360:                        if (rollitems != null && rollitems.length > 1) {
0361:                            BookmarkData b = new BookmarkData(root, // parent
0362:                                    rollitems[0], // name
0363:                                    "", // description
0364:                                    rollitems[1].trim(), // url
0365:                                    null, // feedurl
0366:                                    zero, // weight
0367:                                    zero, // priority
0368:                                    null); // image
0369:                            this .strategy.store(b);
0370:                        }
0371:                    }
0372:                }
0373:
0374:                // add any auto enabled ping targets
0375:                PingTargetManager pingTargetMgr = RollerFactory.getRoller()
0376:                        .getPingTargetManager();
0377:                AutoPingManager autoPingMgr = RollerFactory.getRoller()
0378:                        .getAutopingManager();
0379:
0380:                Iterator pingTargets = pingTargetMgr.getCommonPingTargets()
0381:                        .iterator();
0382:                PingTargetData pingTarget = null;
0383:                while (pingTargets.hasNext()) {
0384:                    pingTarget = (PingTargetData) pingTargets.next();
0385:
0386:                    if (pingTarget.isAutoEnabled()) {
0387:                        AutoPingData autoPing = new AutoPingData(null,
0388:                                pingTarget, newWeblog);
0389:                        autoPingMgr.saveAutoPing(autoPing);
0390:                    }
0391:                }
0392:            }
0393:
0394:            /**
0395:             * Creates and stores a pending PermissionsData for user and website specified.
0396:             * TODO BACKEND: do we really need this?  can't we just use storePermissions()?
0397:             */
0398:            public PermissionsData inviteUser(WebsiteData website,
0399:                    UserData user, short mask) throws RollerException {
0400:
0401:                if (website == null)
0402:                    throw new RollerException("Website cannot be null");
0403:                if (user == null)
0404:                    throw new RollerException("User cannot be null");
0405:
0406:                PermissionsData perms = new PermissionsData();
0407:                perms.setWebsite(website);
0408:                perms.setUser(user);
0409:                perms.setPermissionMask(mask);
0410:                this .strategy.store(perms);
0411:
0412:                return perms;
0413:            }
0414:
0415:            /**
0416:             * Remove user permissions from a website.
0417:             *
0418:             * TODO: replace this with a domain model method like weblog.retireUser(user)
0419:             */
0420:            public void retireUser(WebsiteData website, UserData user)
0421:                    throws RollerException {
0422:
0423:                if (website == null)
0424:                    throw new RollerException("Website cannot be null");
0425:                if (user == null)
0426:                    throw new RollerException("User cannot be null");
0427:
0428:                Iterator perms = website.getPermissions().iterator();
0429:                PermissionsData target = null;
0430:                while (perms.hasNext()) {
0431:                    PermissionsData pd = (PermissionsData) perms.next();
0432:                    if (pd.getUser().getId().equals(user.getId())) {
0433:                        target = pd;
0434:                        break;
0435:                    }
0436:                }
0437:                if (target == null)
0438:                    throw new RollerException("User not member of website");
0439:
0440:                website.removePermission(target);
0441:                this .strategy.remove(target);
0442:            }
0443:
0444:            public WebsiteData getWebsite(String id) throws RollerException {
0445:                return (WebsiteData) this .strategy.load(id, WebsiteData.class);
0446:            }
0447:
0448:            public WebsiteData getWebsiteByHandle(String handle)
0449:                    throws RollerException {
0450:                return getWebsiteByHandle(handle, Boolean.TRUE);
0451:            }
0452:
0453:            /**
0454:             * Return website specified by handle.
0455:             */
0456:            public WebsiteData getWebsiteByHandle(String handle, Boolean enabled)
0457:                    throws RollerException {
0458:
0459:                if (handle == null)
0460:                    throw new RollerException("Handle cannot be null");
0461:
0462:                // check cache first
0463:                // NOTE: if we ever allow changing handles then this needs updating
0464:                if (this .weblogHandleToIdMap.containsKey(handle)) {
0465:
0466:                    WebsiteData weblog = this 
0467:                            .getWebsite((String) this .weblogHandleToIdMap
0468:                                    .get(handle));
0469:                    if (weblog != null) {
0470:                        // only return weblog if enabled status matches
0471:                        if (enabled == null
0472:                                || enabled.equals(weblog.getEnabled())) {
0473:                            log.debug("weblogHandleToId CACHE HIT - " + handle);
0474:                            return weblog;
0475:                        }
0476:                    } else {
0477:                        // mapping hit with lookup miss?  mapping must be old, remove it
0478:                        this .weblogHandleToIdMap.remove(handle);
0479:                    }
0480:                }
0481:
0482:                // cache failed, do lookup
0483:                try {
0484:                    Session session = ((HibernatePersistenceStrategy) this .strategy)
0485:                            .getSession();
0486:                    Criteria criteria = session
0487:                            .createCriteria(WebsiteData.class);
0488:
0489:                    if (enabled != null) {
0490:                        criteria.add(Expression.conjunction().add(
0491:                                new IgnoreCaseEqExpression("handle", handle))
0492:                                .add(Expression.eq("enabled", enabled)));
0493:                    } else {
0494:                        criteria.add(Expression.conjunction().add(
0495:                                Expression.eq("handle", handle)));
0496:                    }
0497:
0498:                    WebsiteData website = (WebsiteData) criteria.uniqueResult();
0499:
0500:                    // add mapping to cache
0501:                    if (website != null) {
0502:                        log.debug("weblogHandleToId CACHE MISS - " + handle);
0503:                        this .weblogHandleToIdMap.put(website.getHandle(),
0504:                                website.getId());
0505:                    }
0506:
0507:                    return website;
0508:                } catch (HibernateException e) {
0509:                    throw new RollerException(e);
0510:                }
0511:            }
0512:
0513:            /**
0514:             * Get websites of a user
0515:             */
0516:            public List getWebsites(UserData user, Boolean enabled,
0517:                    Boolean active, Date startDate, Date endDate, int offset,
0518:                    int length) throws RollerException {
0519:                // TODO: ATLAS getWebsites DONE TESTED
0520:                if (endDate == null)
0521:                    endDate = new Date();
0522:                try {
0523:                    Session session = ((HibernatePersistenceStrategy) this .strategy)
0524:                            .getSession();
0525:                    Criteria criteria = session
0526:                            .createCriteria(WebsiteData.class);
0527:                    if (user != null) {
0528:                        criteria.createAlias("permissions", "permissions");
0529:                        criteria.add(Expression.eq("permissions.user", user));
0530:                        criteria.add(Expression.eq("permissions.pending",
0531:                                Boolean.FALSE));
0532:                    }
0533:                    criteria.add(Expression.lt("dateCreated", endDate));
0534:                    if (startDate != null) {
0535:                        criteria.add(Expression.gt("dateCreated", startDate));
0536:                    }
0537:                    if (enabled != null) {
0538:                        criteria.add(Expression.eq("enabled", enabled));
0539:                    }
0540:                    if (active != null) {
0541:                        criteria.add(Expression.eq("active", active));
0542:                    }
0543:                    if (offset != 0) {
0544:                        criteria.setFirstResult(offset);
0545:                    }
0546:                    if (length != -1) {
0547:                        criteria.setMaxResults(length);
0548:                    }
0549:                    criteria.addOrder(Order.desc("dateCreated"));
0550:                    return criteria.list();
0551:                } catch (HibernateException e) {
0552:                    throw new RollerException(e);
0553:                }
0554:            }
0555:
0556:            public UserData getUser(String id) throws RollerException {
0557:                return (UserData) this .strategy.load(id, UserData.class);
0558:            }
0559:
0560:            public UserData getUserByUserName(String userName)
0561:                    throws RollerException {
0562:                return getUserByUserName(userName, Boolean.TRUE);
0563:            }
0564:
0565:            public UserData getUserByUserName(String userName, Boolean enabled)
0566:                    throws RollerException {
0567:
0568:                if (userName == null)
0569:                    throw new RollerException("userName cannot be null");
0570:
0571:                // check cache first
0572:                // NOTE: if we ever allow changing usernames then this needs updating
0573:                if (this .userNameToIdMap.containsKey(userName)) {
0574:
0575:                    UserData user = this .getUser((String) this .userNameToIdMap
0576:                            .get(userName));
0577:                    if (user != null) {
0578:                        // only return the user if the enabled status matches
0579:                        if (enabled == null
0580:                                || enabled.equals(user.getEnabled())) {
0581:                            log
0582:                                    .debug("userNameToIdMap CACHE HIT - "
0583:                                            + userName);
0584:                            return user;
0585:                        }
0586:                    } else {
0587:                        // mapping hit with lookup miss?  mapping must be old, remove it
0588:                        this .userNameToIdMap.remove(userName);
0589:                    }
0590:                }
0591:
0592:                // cache failed, do lookup
0593:                try {
0594:                    Session session = ((HibernatePersistenceStrategy) this .strategy)
0595:                            .getSession();
0596:                    Criteria criteria = session.createCriteria(UserData.class);
0597:
0598:                    if (enabled != null) {
0599:                        criteria.add(Expression.conjunction().add(
0600:                                Expression.eq("userName", userName)).add(
0601:                                Expression.eq("enabled", enabled)));
0602:                    } else {
0603:                        criteria.add(Expression.conjunction().add(
0604:                                Expression.eq("userName", userName)));
0605:                    }
0606:
0607:                    UserData user = (UserData) criteria.uniqueResult();
0608:
0609:                    // add mapping to cache
0610:                    if (user != null) {
0611:                        log.debug("userNameToIdMap CACHE MISS - " + userName);
0612:                        this .userNameToIdMap.put(user.getUserName(), user
0613:                                .getId());
0614:                    }
0615:
0616:                    return user;
0617:                } catch (HibernateException e) {
0618:                    throw new RollerException(e);
0619:                }
0620:            }
0621:
0622:            public List getUsers(int offset, int length) throws RollerException {
0623:                return getUsers(Boolean.TRUE, null, null, offset, length);
0624:            }
0625:
0626:            public List getUsers(Boolean enabled, Date startDate, Date endDate,
0627:                    int offset, int length) throws RollerException {
0628:                if (endDate == null)
0629:                    endDate = new Date();
0630:                try {
0631:                    Session session = ((HibernatePersistenceStrategy) this .strategy)
0632:                            .getSession();
0633:                    Criteria criteria = session.createCriteria(UserData.class);
0634:                    criteria.add(Expression.lt("dateCreated", endDate));
0635:                    if (startDate != null) {
0636:                        criteria.add(Expression.gt("dateCreated", startDate));
0637:                    }
0638:                    if (enabled != null) {
0639:                        criteria.add(Expression.eq("enabled", enabled));
0640:                    }
0641:                    if (offset != 0) {
0642:                        criteria.setFirstResult(offset);
0643:                    }
0644:                    if (length != -1) {
0645:                        criteria.setMaxResults(length);
0646:                    }
0647:                    criteria.addOrder(Order.desc("dateCreated"));
0648:                    return criteria.list();
0649:                } catch (HibernateException e) {
0650:                    throw new RollerException(e);
0651:                }
0652:            }
0653:
0654:            /**
0655:             * Get users of a website
0656:             */
0657:            public List getUsers(WebsiteData website, Boolean enabled,
0658:                    int offset, int length) throws RollerException {
0659:
0660:                try {
0661:                    Session session = ((HibernatePersistenceStrategy) this .strategy)
0662:                            .getSession();
0663:                    Criteria criteria = session.createCriteria(UserData.class);
0664:                    if (website != null) {
0665:                        criteria.createAlias("permissions", "permissions");
0666:                        criteria.add(Expression.eq("permissions.website",
0667:                                website));
0668:                    }
0669:                    if (enabled != null) {
0670:                        criteria.add(Expression.eq("enabled", enabled));
0671:                    }
0672:                    if (offset != 0) {
0673:                        criteria.setFirstResult(offset);
0674:                    }
0675:                    if (length != -1) {
0676:                        criteria.setMaxResults(length);
0677:                    }
0678:                    return criteria.list();
0679:                } catch (HibernateException e) {
0680:                    throw new RollerException(e);
0681:                }
0682:            }
0683:
0684:            public List getUsersStartingWith(String startsWith,
0685:                    Boolean enabled, int offset, int length)
0686:                    throws RollerException {
0687:
0688:                List results = new ArrayList();
0689:                try {
0690:                    Session session = ((HibernatePersistenceStrategy) this .strategy)
0691:                            .getSession();
0692:                    Criteria criteria = session.createCriteria(UserData.class);
0693:
0694:                    if (enabled != null) {
0695:                        criteria.add(Expression.eq("enabled", enabled));
0696:                    }
0697:                    if (startsWith != null) {
0698:                        criteria.add(Expression.disjunction().add(
0699:                                Expression.like("userName", startsWith,
0700:                                        MatchMode.START)).add(
0701:                                Expression.like("emailAddress", startsWith,
0702:                                        MatchMode.START)));
0703:                    }
0704:                    if (offset != 0) {
0705:                        criteria.setFirstResult(offset);
0706:                    }
0707:                    if (length != -1) {
0708:                        criteria.setMaxResults(length);
0709:                    }
0710:                    results = criteria.list();
0711:                } catch (HibernateException e) {
0712:                    throw new RollerException(e);
0713:                }
0714:                return results;
0715:            }
0716:
0717:            public WeblogTemplate getPage(String id) throws RollerException {
0718:                // Don't hit database for templates stored on disk
0719:                if (id != null && id.endsWith(".vm"))
0720:                    return null;
0721:
0722:                return (WeblogTemplate) this .strategy.load(id,
0723:                        WeblogTemplate.class);
0724:            }
0725:
0726:            /**
0727:             * Use Hibernate directly because Roller's Query API does too much allocation.
0728:             */
0729:            public WeblogTemplate getPageByLink(WebsiteData website,
0730:                    String pagelink) throws RollerException {
0731:
0732:                if (website == null)
0733:                    throw new RollerException("userName is null");
0734:
0735:                if (pagelink == null)
0736:                    throw new RollerException("Pagelink is null");
0737:
0738:                try {
0739:                    Session session = ((HibernatePersistenceStrategy) this .strategy)
0740:                            .getSession();
0741:                    Criteria criteria = session
0742:                            .createCriteria(WeblogTemplate.class);
0743:                    criteria.add(Expression.eq("website", website));
0744:                    criteria.add(Expression.eq("link", pagelink));
0745:                    criteria.setMaxResults(1);
0746:
0747:                    List list = criteria.list();
0748:                    return list.size() != 0 ? (WeblogTemplate) list.get(0)
0749:                            : null;
0750:                } catch (HibernateException e) {
0751:                    throw new RollerException(e);
0752:                }
0753:            }
0754:
0755:            /**
0756:             * @see org.apache.roller.model.UserManager#getPageByName(WebsiteData, java.lang.String)
0757:             */
0758:            public WeblogTemplate getPageByName(WebsiteData website,
0759:                    String pagename) throws RollerException {
0760:
0761:                if (website == null)
0762:                    throw new RollerException("website is null");
0763:
0764:                if (pagename == null)
0765:                    throw new RollerException("Page name is null");
0766:
0767:                try {
0768:                    Session session = ((HibernatePersistenceStrategy) this .strategy)
0769:                            .getSession();
0770:                    Criteria criteria = session
0771:                            .createCriteria(WeblogTemplate.class);
0772:                    criteria.add(Expression.eq("website", website));
0773:                    criteria.add(Expression.eq("name", pagename));
0774:                    criteria.setMaxResults(1);
0775:
0776:                    List list = criteria.list();
0777:                    return list.size() != 0 ? (WeblogTemplate) list.get(0)
0778:                            : null;
0779:                } catch (HibernateException e) {
0780:                    throw new RollerException(e);
0781:                }
0782:            }
0783:
0784:            /**
0785:             * @see org.apache.roller.model.UserManager#getPages(WebsiteData)
0786:             */
0787:            public List getPages(WebsiteData website) throws RollerException {
0788:
0789:                if (website == null)
0790:                    throw new RollerException("website is null");
0791:
0792:                try {
0793:                    Session session = ((HibernatePersistenceStrategy) this .strategy)
0794:                            .getSession();
0795:                    Criteria criteria = session
0796:                            .createCriteria(WeblogTemplate.class);
0797:                    criteria.add(Expression.eq("website", website));
0798:                    criteria.addOrder(Order.asc("name"));
0799:
0800:                    return criteria.list();
0801:                } catch (HibernateException e) {
0802:                    throw new RollerException(e);
0803:                }
0804:            }
0805:
0806:            public PermissionsData getPermissions(String inviteId)
0807:                    throws RollerException {
0808:                return (PermissionsData) this .strategy.load(inviteId,
0809:                        PermissionsData.class);
0810:            }
0811:
0812:            /**
0813:             * Return permissions for specified user in website
0814:             */
0815:            public PermissionsData getPermissions(WebsiteData website,
0816:                    UserData user) throws RollerException {
0817:
0818:                try {
0819:                    Session session = ((HibernatePersistenceStrategy) this .strategy)
0820:                            .getSession();
0821:                    Criteria criteria = session
0822:                            .createCriteria(PermissionsData.class);
0823:                    criteria.add(Expression.eq("website", website));
0824:                    criteria.add(Expression.eq("user", user));
0825:
0826:                    List list = criteria.list();
0827:                    return list.size() != 0 ? (PermissionsData) list.get(0)
0828:                            : null;
0829:                } catch (HibernateException e) {
0830:                    throw new RollerException(e);
0831:                }
0832:            }
0833:
0834:            /**
0835:             * Get pending permissions for user
0836:             */
0837:            public List getPendingPermissions(UserData user)
0838:                    throws RollerException {
0839:
0840:                try {
0841:                    Session session = ((HibernatePersistenceStrategy) this .strategy)
0842:                            .getSession();
0843:                    Criteria criteria = session
0844:                            .createCriteria(PermissionsData.class);
0845:                    criteria.add(Expression.eq("user", user));
0846:                    criteria.add(Expression.eq("pending", Boolean.TRUE));
0847:
0848:                    return criteria.list();
0849:                } catch (HibernateException e) {
0850:                    throw new RollerException(e);
0851:                }
0852:            }
0853:
0854:            /**
0855:             * Get pending permissions for website
0856:             */
0857:            public List getPendingPermissions(WebsiteData website)
0858:                    throws RollerException {
0859:
0860:                try {
0861:                    Session session = ((HibernatePersistenceStrategy) this .strategy)
0862:                            .getSession();
0863:                    Criteria criteria = session
0864:                            .createCriteria(PermissionsData.class);
0865:                    criteria.add(Expression.eq("website", website));
0866:                    criteria.add(Expression.eq("pending", Boolean.TRUE));
0867:
0868:                    return criteria.list();
0869:                } catch (HibernateException e) {
0870:                    throw new RollerException(e);
0871:                }
0872:            }
0873:
0874:            /**
0875:             * Get all permissions of a website (pendings not including)
0876:             */
0877:            public List getAllPermissions(WebsiteData website)
0878:                    throws RollerException {
0879:
0880:                try {
0881:                    Session session = ((HibernatePersistenceStrategy) this .strategy)
0882:                            .getSession();
0883:                    Criteria criteria = session
0884:                            .createCriteria(PermissionsData.class);
0885:                    criteria.add(Expression.eq("website", website));
0886:                    criteria.add(Expression.eq("pending", Boolean.FALSE));
0887:
0888:                    return criteria.list();
0889:                } catch (HibernateException e) {
0890:                    throw new RollerException(e);
0891:                }
0892:            }
0893:
0894:            /**
0895:             * Get all permissions of a user.
0896:             */
0897:            public List getAllPermissions(UserData user) throws RollerException {
0898:
0899:                try {
0900:                    Session session = ((HibernatePersistenceStrategy) this .strategy)
0901:                            .getSession();
0902:                    Criteria criteria = session
0903:                            .createCriteria(PermissionsData.class);
0904:                    criteria.add(Expression.eq("user", user));
0905:                    criteria.add(Expression.eq("pending", Boolean.FALSE));
0906:
0907:                    return criteria.list();
0908:                } catch (HibernateException e) {
0909:                    throw new RollerException(e);
0910:                }
0911:            }
0912:
0913:            public void release() {
0914:            }
0915:
0916:            public Map getUserNameLetterMap() throws RollerException {
0917:                // TODO: ATLAS getUserNameLetterMap DONE TESTED
0918:                String msg = "Getting username letter map";
0919:                try {
0920:                    String lc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
0921:                    Map results = new TreeMap();
0922:                    Session session = ((HibernatePersistenceStrategy) strategy)
0923:                            .getSession();
0924:                    for (int i = 0; i < 26; i++) {
0925:                        Query query = session
0926:                                .createQuery("select count(user) from UserData user where upper(user.userName) like '"
0927:                                        + lc.charAt(i) + "%'");
0928:                        List row = query.list();
0929:                        Integer count = (Integer) row.get(0);
0930:                        results.put(new String(new char[] { lc.charAt(i) }),
0931:                                count);
0932:                    }
0933:                    return results;
0934:                } catch (Throwable pe) {
0935:                    log.error(msg, pe);
0936:                    throw new RollerException(msg, pe);
0937:                }
0938:            }
0939:
0940:            public List getUsersByLetter(char letter, int offset, int length)
0941:                    throws RollerException {
0942:                // TODO: ATLAS getUsersByLetter DONE
0943:                try {
0944:                    Session session = ((HibernatePersistenceStrategy) this .strategy)
0945:                            .getSession();
0946:                    Criteria criteria = session.createCriteria(UserData.class);
0947:                    criteria.add(Expression.ilike("userName", new String(
0948:                            new char[] { letter })
0949:                            + "%", MatchMode.START));
0950:                    criteria.addOrder(Order.asc("userName"));
0951:                    if (offset != 0) {
0952:                        criteria.setFirstResult(offset);
0953:                    }
0954:                    if (length != -1) {
0955:                        criteria.setMaxResults(length);
0956:                    }
0957:                    return criteria.list();
0958:                } catch (HibernateException e) {
0959:                    throw new RollerException(e);
0960:                }
0961:            }
0962:
0963:            public Map getWeblogHandleLetterMap() throws RollerException {
0964:                // TODO: ATLAS getWeblogHandleLetterMap DONE
0965:                String msg = "Getting weblog letter map";
0966:                try {
0967:                    String lc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
0968:                    Map results = new TreeMap();
0969:                    Session session = ((HibernatePersistenceStrategy) strategy)
0970:                            .getSession();
0971:                    for (int i = 0; i < 26; i++) {
0972:                        Query query = session
0973:                                .createQuery("select count(website) from WebsiteData website where upper(website.handle) like '"
0974:                                        + lc.charAt(i) + "%'");
0975:                        List row = query.list();
0976:                        Integer count = (Integer) row.get(0);
0977:                        results.put(new String(new char[] { lc.charAt(i) }),
0978:                                count);
0979:                    }
0980:                    return results;
0981:                } catch (Throwable pe) {
0982:                    log.error(msg, pe);
0983:                    throw new RollerException(msg, pe);
0984:                }
0985:            }
0986:
0987:            public List getWeblogsByLetter(char letter, int offset, int length)
0988:                    throws RollerException {
0989:                // TODO: ATLAS getWeblogsByLetter DONE
0990:                try {
0991:                    Session session = ((HibernatePersistenceStrategy) this .strategy)
0992:                            .getSession();
0993:                    Criteria criteria = session
0994:                            .createCriteria(WebsiteData.class);
0995:                    criteria.add(Expression.ilike("handle", new String(
0996:                            new char[] { letter })
0997:                            + "%", MatchMode.START));
0998:                    criteria.addOrder(Order.asc("handle"));
0999:                    if (offset != 0) {
1000:                        criteria.setFirstResult(offset);
1001:                    }
1002:                    if (length != -1) {
1003:                        criteria.setMaxResults(length);
1004:                    }
1005:                    return criteria.list();
1006:                } catch (HibernateException e) {
1007:                    throw new RollerException(e);
1008:                }
1009:            }
1010:
1011:            public List getMostCommentedWebsites(Date startDate, Date endDate,
1012:                    int offset, int length) throws RollerException {
1013:                // TODO: ATLAS getMostCommentedWebsites DONE TESTED
1014:                String msg = "Getting most commented websites";
1015:                if (endDate == null)
1016:                    endDate = new Date();
1017:                try {
1018:                    Session session = ((HibernatePersistenceStrategy) strategy)
1019:                            .getSession();
1020:                    StringBuffer sb = new StringBuffer();
1021:                    sb
1022:                            .append("select count(distinct c), c.weblogEntry.website.id, c.weblogEntry.website.handle, c.weblogEntry.website.name ");
1023:                    sb
1024:                            .append("from CommentData c where c.weblogEntry.pubTime < :endDate ");
1025:                    if (startDate != null) {
1026:                        sb.append("and c.weblogEntry.pubTime > :startDate ");
1027:                    }
1028:                    sb
1029:                            .append("group by c.weblogEntry.website.id, c.weblogEntry.website.name, c.weblogEntry.website.description order by col_0_0_ desc");
1030:                    Query query = session.createQuery(sb.toString());
1031:                    query.setParameter("endDate", endDate);
1032:                    if (startDate != null) {
1033:                        query.setParameter("startDate", startDate);
1034:                    }
1035:                    if (offset != 0) {
1036:                        query.setFirstResult(offset);
1037:                    }
1038:                    if (length != -1) {
1039:                        query.setMaxResults(length);
1040:                    }
1041:                    List results = new ArrayList();
1042:                    for (Iterator iter = query.list().iterator(); iter
1043:                            .hasNext();) {
1044:                        Object[] row = (Object[]) iter.next();
1045:                        results.add(new StatCount((String) row[1],
1046:                                (String) row[2], (String) row[3],
1047:                                "statCount.weblogCommentCountType", new Long(
1048:                                        ((Integer) row[0]).intValue())
1049:                                        .longValue()));
1050:                    }
1051:                    return results;
1052:                } catch (Throwable pe) {
1053:                    log.error(msg, pe);
1054:                    throw new RollerException(msg, pe);
1055:                }
1056:            }
1057:
1058:            /** Doesn't seem to be any other way to get ignore case w/o QBE */
1059:            class IgnoreCaseEqExpression extends SimpleExpression {
1060:                public IgnoreCaseEqExpression(String property, Object value) {
1061:                    super (property, value, "=", true);
1062:                }
1063:            }
1064:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.