001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.upgrade.v4_4_0.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.upgrade.StagnantRowException;
021    import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
022    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portlet.blogs.model.BlogsEntry;
025    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
026    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
027    import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
028    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
029    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
030    import com.liferay.portlet.imagegallery.model.IGImage;
031    import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
032    import com.liferay.portlet.journal.model.JournalArticle;
033    import com.liferay.portlet.journal.model.JournalArticleResource;
034    import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
035    import com.liferay.portlet.messageboards.model.MBMessage;
036    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
037    import com.liferay.portlet.wiki.model.WikiNode;
038    import com.liferay.portlet.wiki.model.WikiPage;
039    import com.liferay.portlet.wiki.model.WikiPageResource;
040    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
041    import com.liferay.portlet.wiki.service.WikiPageResourceLocalServiceUtil;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     */
046    public class TagsAssetGroupIdUpgradeColumnImpl extends BaseUpgradeColumnImpl {
047    
048            public TagsAssetGroupIdUpgradeColumnImpl(
049                    UpgradeColumn classNameIdColumn, UpgradeColumn classPKColumn) {
050    
051                    super("groupId");
052    
053                    _classNameIdColumn = classNameIdColumn;
054                    _classPKColumn = classPKColumn;
055            }
056    
057            public Object getNewValue(Object oldValue) throws Exception {
058                    Long oldGroupId = (Long)oldValue;
059    
060                    Long newGroupId = oldGroupId;
061    
062                    if (oldGroupId.longValue() == 0) {
063                            Long classNameId = (Long)_classNameIdColumn.getOldValue();
064                            Long classPK = (Long)_classPKColumn.getOldValue();
065    
066                            String className = PortalUtil.getClassName(classNameId.longValue());
067    
068                            try {
069                                    newGroupId = new Long(
070                                            getGroupId(className, classPK.longValue()));
071                            }
072                            catch (PortalException pe) {
073                                    throw new StagnantRowException(pe.getMessage(), pe);
074                            }
075                            catch (Exception e) {
076                                    if (_log.isWarnEnabled()) {
077                                            _log.warn(e, e);
078                                    }
079                            }
080                    }
081    
082                    return newGroupId;
083            }
084    
085            protected long getGroupId(String className, long classPK) throws Exception {
086                    if (className.equals(BlogsEntry.class.getName())) {
087                            BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
088    
089                            return entry.getGroupId();
090                    }
091                    else if (className.equals(BookmarksEntry.class.getName())) {
092                            BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(
093                                    classPK);
094    
095                            return entry.getFolder().getGroupId();
096                    }
097                    else if (className.equals(DLFileEntry.class.getName())) {
098                            DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
099                                    classPK);
100    
101                            return fileEntry.getFolder().getGroupId();
102                    }
103                    else if (className.equals(IGImage.class.getName())) {
104                            IGImage image = IGImageLocalServiceUtil.getImage(classPK);
105    
106                            return image.getFolder().getGroupId();
107                    }
108                    else if (className.equals(JournalArticle.class.getName())) {
109                            JournalArticleResource articleResource =
110                                    JournalArticleResourceLocalServiceUtil.getArticleResource(
111                                            classPK);
112    
113                            return articleResource.getGroupId();
114                    }
115                    else if (className.equals(MBMessage.class.getName())) {
116                            MBMessage message = MBMessageLocalServiceUtil.getMessage(classPK);
117    
118                            return message.getCategory().getGroupId();
119                    }
120                    else if (className.equals(WikiPage.class.getName())) {
121                            WikiPageResource pageResource =
122                                    WikiPageResourceLocalServiceUtil.getPageResource(classPK);
123    
124                            WikiNode node = WikiNodeLocalServiceUtil.getNode(
125                                    pageResource.getNodeId());
126    
127                            return node.getGroupId();
128                    }
129                    else {
130                            return 0;
131                    }
132            }
133    
134            private static Log _log = LogFactoryUtil.getLog(
135                    TagsAssetGroupIdUpgradeColumnImpl.class);
136    
137            private UpgradeColumn _classNameIdColumn;
138            private UpgradeColumn _classPKColumn;
139    
140    }