databasepsmlmanagerservice.java

来自「jetspeed源代码」· Java 代码 · 共 1,277 行 · 第 1/3 页

JAVA
1,277
字号
            if (colonToken.equals("User"))
            {
                entity = colonTokens.nextToken().trim();
                locator.setUser(JetspeedSecurity.getUser(entity));
            }
            else if (colonToken.equals("Group"))
            {
                entity = colonTokens.nextToken().trim();
                locator.setGroup(JetspeedSecurity.getGroup(entity));
            }
            else if (colonToken.equals("Role"))
            {
                entity = colonTokens.nextToken().trim();
                locator.setRole(JetspeedSecurity.getRole(entity));
            }
            else if (colonToken.equals("Page"))
            {
                entity = colonTokens.nextToken().trim();
                locator.setName(entity);
            }
            else if (colonToken.equals("MediaType"))
            {
                entity = colonTokens.nextToken().trim();
                locator.setMediaType(entity);
            }
            else if (colonToken.equals("Country"))
            {
                entity = colonTokens.nextToken().trim();
                locator.setCountry(entity);
            }
            else if (colonToken.equals("Language"))
            {
                entity = colonTokens.nextToken().trim();
                locator.setLanguage(entity);
            }
        }
        if (logger.isDebugEnabled())
            logger.debug("DatabasePsmlManagerService: Returning locator for string: " + locatorToString(locator));

        return locator;

    }

    public PSMLDocument getDocument(String name)
    {
        // do nothing, deprecated
        logger.warn("*** NOT SUPPORTED: GETDOC FROM DATABASE PSML MANAGER!!!");
        return null;
    }

    public boolean saveDocument(String fileOrUrl, PSMLDocument doc)
    {
        // do nothing, deprecated
        logger.warn("*** NOT SUPPORTED: SAVING DOC FROM DATABASE PSML MANAGER!!!");
        return false;
    }

    public boolean saveDocument(PSMLDocument doc)
    {
        // do nothing, will be deprecated
        logger.warn("*** NOT SUPPORTED: SAVING DOC FROM DATABASE PSML MANAGER!!!");
        return false;
    }


    /**
     * Returns a PSML document for the given locator
     *
     * @param locator The locator descriptor(ProfileLocator object) of the
     * document to be retrieved.
     * @return psmldoc The PSMLDocument object
     */
    public PSMLDocument getDocument(ProfileLocator locator)
    {
        // check the cache for the req'e document if not available in cache
        // get the document from database

        if (locator == null)
        {
            String message = "PSMLManager: Must specify a locator";
            logger.warn("DatabasePsmlManagerService.getDocument: " + message);
            throw new IllegalArgumentException(message);
        }

        PSMLDocument psmldoc = null;
        String locStr = locatorToString(locator);
        boolean inCache = false;

        if (cachingOn)
        {
            synchronized (psmlCache)
            {
                // psmldoc = (PSMLDocument)psmlCache.get(locatorToString(locator));
                // if we have seached and found nothing, this is cached as a null value
                // so check to see if the key is there
                inCache = psmlCache.containsKey(locStr);
                if (inCache)
                {
                    psmldoc = (PSMLDocument)psmlCache.get(locStr);
                }
             }
//        if (Log.getLogger().isDebugEnabled())
//            Log.info("DatabasePsmlManagerService.getDocument(): psmlcache: " +
//                (inCache ? ((psmldoc == null) ? "null present" : "doc present") : "not in cache") + " : " + locStr);

            // if in the cache, doc or null, return what's in the cache
            if (inCache)
            {
                return psmldoc;
            }
        }


        try
        {
            return refresh(locator);
        }
        catch (Exception e)
        {
            logger.warn("DatabasePSMLManagerService.getDocument: exception:", e);
            throw new RuntimeException("Could not get profile from DB");
        }
    }

    /**
     * Stores the PSML document in DB for the given profile
     *
     * @param profile The profile that holds the PSMLDocument.
     * @return PSMLDocument The PSMLDocument that got created in DB.
     */
    public PSMLDocument createDocument(Profile profile)
    {
        return createOrSaveDocument(profile, INSERT);
    }

    /**
     * Update the PSML document in DB for the given profile
     *
     * @param profile The profile that holds the PSMLDocument.
     * @return PSMLDocument The PSMLDocument that got created in DB.
     */
    public boolean store(Profile profile)
    {
        return createOrSaveDocument(profile, UPDATE) != null;
    }

    private PSMLDocument createOrSaveDocument(Profile profile, int operation)
    {
        // create record in the database for Portlets for the given
        // profile/PSMLDocuemnt,use marsheller to create Portlets
        // object and then put it in database, update the cache
        if (profile == null)
        {
            String message = "PSMLManager: Must specify a profile";
            logger.warn("DatabasePsmlManagerService.createOrSaveDocument: " + message);
            throw new IllegalArgumentException(message);
        }

        JetspeedUser user = profile.getUser();
        Role role = profile.getRole();
        Group group = profile.getGroup();
        String tableName = null;

        Connection dbCon = getDbConnection();

        try
        {
            if (user != null)
            {
                tableName = "JETSPEED_USER_PROFILE";
                if (operation == INSERT)
                {
                    new JetspeedUserProfilePeer().insert(profile, dbCon);
                }
                else if (operation == UPDATE)
                {
                    new JetspeedUserProfilePeer().update(profile, dbCon);
                }
            }
            else if (role != null)
            {
                tableName = "JETSPEED_ROLE_PROFILE";
                if (operation == INSERT)
                {
                    new JetspeedRoleProfilePeer().insert(profile, dbCon);
                }
                else if (operation == UPDATE)
                {
                    new JetspeedRoleProfilePeer().update(profile, dbCon);
                }
            }
            else if (group != null)
            {
                tableName = "JETSPEED_GROUP_PROFILE";
                if (operation == INSERT)
                {
                    new JetspeedGroupProfilePeer().insert(profile, dbCon);
                }
                else if (operation == UPDATE)
                {
                    new JetspeedGroupProfilePeer().update(profile, dbCon);
                }
            }

            if (cachingOn)
            {
                // insert successful
                synchronized (psmlCache)
                {
                    if (logger.isDebugEnabled())
                        logger.debug("DatabasePsmlManagerService.createOrSaveDocument: caching document: profile: " + locatorToString(profile));
                    psmlCache.put(locatorToString(profile), profile.getDocument());
                }
            }

            return profile.getDocument();
        }
        catch (Exception e) // insert failed
        {
            logger.warn("DatabasePsmlManagerService.createOrSaveDocument: profile: "
                        + profile + " tableName: " + tableName, e);
            throw new RuntimeException("Could not create new profile in DB");
        }
        finally
        {
            // make sure to release the database connection
            releaseDbConnection(dbCon);
        }

    }

    /**
     * Remove the PSMLDocument/profile for given locator object.
     *
     * @param locator The profile locator criteria for profile to be removed.
     */
    public void removeDocument(ProfileLocator locator)
    {
        if (locator == null)
        {
            String message = "PSMLManager: Must specify a locator";
            logger.warn("DatabasePsmlManagerService.removeDocument: " + message);
            throw new IllegalArgumentException(message);
        }

        JetspeedUser user = locator.getUser();
        Role role = locator.getRole();
        Group group = locator.getGroup();
        String tableName = null;

        // get a database connection
        Connection dbCon = getDbConnection();

        try
        {
            if (user != null)
            {
                new JetspeedUserProfilePeer().delete(locator, dbCon);
                tableName = "JETSPEED_USER_PROFILE";
            }
            else if (role != null)
            {
                new JetspeedRoleProfilePeer().delete(locator, dbCon);
                tableName = "JETSPEED_ROLE_PROFILE";
            }
            else if (group != null)
            {
                new JetspeedGroupProfilePeer().delete(locator, dbCon);
                tableName = "JETSPEED_GROUP_PROFILE";
            }

            if (cachingOn)
            {
                // Delete successful
                synchronized (psmlCache)
                {
                    psmlCache.remove(locatorToString(locator));
                }
            }
        }
        catch (Exception e) // insert failed
        {
            logger.warn("DatabasePsmlManagerService.removeDocument: profile: "
                        + locatorToString(locator) + " tableName: " + tableName, e);
            throw new RuntimeException("Could not delete profile for given locator from DB");
        }
        finally
        {
            // make sure to release the database connection
            releaseDbConnection(dbCon);
        }
    }

    /**
     * Query for a collection of profiles given a profile locator criteria.
     * Use SQL engine to get the required profiles.
     *
     * @param locator The profile locator criteria.
     * @return Iterator object with the PSMLDocuments satisfying query
     */
    public Iterator query(QueryLocator locator)
    {
        if (locator == null)
        {
            String message = "PSMLManager: Must specify a locator";
            logger.warn("DatabasePsmlManagerService.query: " + message);
            throw new IllegalArgumentException(message);
        }

        Connection dbCon = getDbConnection();

        try
        {
            List userData = null;
            List groupData = null;
            List roleData = null;

            int queryMode = locator.getQueryMode();

            List list = new ArrayList();

            switch (queryMode)
            {
                case QueryLocator.QUERY_USER:
                    userData = new JetspeedUserProfilePeer().selectOrdered(locator, dbCon);
                    if (userData != null)
                    {
                        list = getProfiles(userData);
                    }
                    break;

                case QueryLocator.QUERY_GROUP:
                    groupData = new JetspeedGroupProfilePeer().selectOrdered(locator, dbCon);
                    if (groupData != null)
                    {
                        list = getProfiles(groupData);
                    }
                    break;

                case QueryLocator.QUERY_ROLE:
                    roleData = new JetspeedRoleProfilePeer().selectOrdered(locator, dbCon);
                    if (roleData != null)
                    {
                        list = getProfiles(roleData);
                    }
                    break;


                default:  //QUERY_ALL
                    userData = new JetspeedUserProfilePeer().selectOrdered(locator, dbCon);
                    if (userData != null)
                    {
                        list.addAll(getProfiles(userData));
                    }

                    groupData = new JetspeedGroupProfilePeer().selectOrdered(locator, dbCon);
                    if (groupData != null)
                    {
                        list.addAll(getProfiles(groupData));
                    }

                    roleData = new JetspeedRoleProfilePeer().selectOrdered(locator, dbCon);
                    if (roleData != null)
                    {
                        list.addAll(getProfiles(roleData));
                    }

                   break;
            }

            return list.iterator();
        }
        catch (Exception e)
        {
            logger.warn("DatabasePsmlManagerService.query: exception" , e);
        }
        finally
        {
            // make sure to release the databased connection
            releaseDbConnection(dbCon);
        }

        return new ArrayList().iterator();  // return empty non-null iterator
    }

    /**
     * Get profile iterator from given list of objects.
     *
     * @param data List of JetspeedUserProfile, JetspeedGroupProfile,
     * JetspeedRoleProfile, objects
     * @return List of profiles
     */
    private List getProfiles(List data)
    {
        List list = new ArrayList();

        for (int i = 0; i < data.size(); i++)
        {
            Object obj = data.get(i);
            Portlets portlets = null;

            if (obj instanceof JetspeedUserProfile)
            {
                portlets = DBUtils.bytesToPortlets(((JetspeedUserProfile)obj).getProfile(), this.mapping);
                list.add(createUserProfile((JetspeedUserProfile)obj, portlets));
            }
            else if (obj instanceof JetspeedGroupProfile)
            {
                portlets = DBUtils.bytesToPortlets(((JetspeedGroupProfile)obj).getProfile(), this.mapping);
                list.add(createGroupProfile((JetspeedGroupProfile)obj, portlets));
            }
            else if (obj instanceof JetspeedRoleProfile)
            {
                portlets = DBUtils.bytesToPortlets(((JetspeedRoleProfile)obj).getProfile(), this.mapping);
                list.add(createRoleProfile((JetspeedRoleProfile)obj, portlets));
            }

        }
        return list;
    }


    /**
     * Get PSMLDocument object for given pagename and portlets.
     *
     * @param portlets Portlets for the given page name

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?