1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.upgrade.v4_4_0.util;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.upgrade.StagnantRowException;
21  import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
22  import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portlet.blogs.model.BlogsEntry;
25  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
26  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
27  import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
28  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
29  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
30  import com.liferay.portlet.imagegallery.model.IGImage;
31  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
32  import com.liferay.portlet.journal.model.JournalArticle;
33  import com.liferay.portlet.journal.model.JournalArticleResource;
34  import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
35  import com.liferay.portlet.messageboards.model.MBMessage;
36  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
37  import com.liferay.portlet.wiki.model.WikiNode;
38  import com.liferay.portlet.wiki.model.WikiPage;
39  import com.liferay.portlet.wiki.model.WikiPageResource;
40  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
41  import com.liferay.portlet.wiki.service.WikiPageResourceLocalServiceUtil;
42  
43  /**
44   * <a href="TagsAssetGroupIdUpgradeColumnImpl.java.html"><b><i>View Source</i>
45   * </b></a>
46   *
47   * @author Brian Wing Shun Chan
48   */
49  public class TagsAssetGroupIdUpgradeColumnImpl extends BaseUpgradeColumnImpl {
50  
51      public TagsAssetGroupIdUpgradeColumnImpl(
52          UpgradeColumn classNameIdColumn, UpgradeColumn classPKColumn) {
53  
54          super("groupId");
55  
56          _classNameIdColumn = classNameIdColumn;
57          _classPKColumn = classPKColumn;
58      }
59  
60      public Object getNewValue(Object oldValue) throws Exception {
61          Long oldGroupId = (Long)oldValue;
62  
63          Long newGroupId = oldGroupId;
64  
65          if (oldGroupId.longValue() == 0) {
66              Long classNameId = (Long)_classNameIdColumn.getOldValue();
67              Long classPK = (Long)_classPKColumn.getOldValue();
68  
69              String className = PortalUtil.getClassName(classNameId.longValue());
70  
71              try {
72                  newGroupId = new Long(
73                      getGroupId(className, classPK.longValue()));
74              }
75              catch (PortalException pe) {
76                  throw new StagnantRowException(pe.getMessage(), pe);
77              }
78              catch (Exception e) {
79                  if (_log.isWarnEnabled()) {
80                      _log.warn(e, e);
81                  }
82              }
83          }
84  
85          return newGroupId;
86      }
87  
88      protected long getGroupId(String className, long classPK) throws Exception {
89          if (className.equals(BlogsEntry.class.getName())) {
90              BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
91  
92              return entry.getGroupId();
93          }
94          else if (className.equals(BookmarksEntry.class.getName())) {
95              BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(
96                  classPK);
97  
98              return entry.getFolder().getGroupId();
99          }
100         else if (className.equals(DLFileEntry.class.getName())) {
101             DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
102                 classPK);
103 
104             return fileEntry.getFolder().getGroupId();
105         }
106         else if (className.equals(IGImage.class.getName())) {
107             IGImage image = IGImageLocalServiceUtil.getImage(classPK);
108 
109             return image.getFolder().getGroupId();
110         }
111         else if (className.equals(JournalArticle.class.getName())) {
112             JournalArticleResource articleResource =
113                 JournalArticleResourceLocalServiceUtil.getArticleResource(
114                     classPK);
115 
116             return articleResource.getGroupId();
117         }
118         else if (className.equals(MBMessage.class.getName())) {
119             MBMessage message = MBMessageLocalServiceUtil.getMessage(classPK);
120 
121             return message.getCategory().getGroupId();
122         }
123         else if (className.equals(WikiPage.class.getName())) {
124             WikiPageResource pageResource =
125                 WikiPageResourceLocalServiceUtil.getPageResource(classPK);
126 
127             WikiNode node = WikiNodeLocalServiceUtil.getNode(
128                 pageResource.getNodeId());
129 
130             return node.getGroupId();
131         }
132         else {
133             return 0;
134         }
135     }
136 
137     private static Log _log = LogFactoryUtil.getLog(
138         TagsAssetGroupIdUpgradeColumnImpl.class);
139 
140     private UpgradeColumn _classNameIdColumn;
141     private UpgradeColumn _classPKColumn;
142 
143 }