1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.upgrade.v4_4_0.util;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.upgrade.StagnantRowException;
29  import com.liferay.portal.upgrade.util.BaseUpgradeColumnImpl;
30  import com.liferay.portal.upgrade.util.UpgradeColumn;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portlet.blogs.model.BlogsEntry;
33  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
34  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
35  import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
36  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
37  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
38  import com.liferay.portlet.imagegallery.model.IGImage;
39  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
40  import com.liferay.portlet.journal.model.JournalArticle;
41  import com.liferay.portlet.journal.model.JournalArticleResource;
42  import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
43  import com.liferay.portlet.messageboards.model.MBMessage;
44  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
45  import com.liferay.portlet.wiki.model.WikiNode;
46  import com.liferay.portlet.wiki.model.WikiPage;
47  import com.liferay.portlet.wiki.model.WikiPageResource;
48  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
49  import com.liferay.portlet.wiki.service.WikiPageResourceLocalServiceUtil;
50  
51  /**
52   * <a href="TagsAssetGroupIdUpgradeColumnImpl.java.html"><b><i>View Source</i>
53   * </b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class TagsAssetGroupIdUpgradeColumnImpl extends BaseUpgradeColumnImpl {
59  
60      public TagsAssetGroupIdUpgradeColumnImpl(
61          UpgradeColumn classNameIdColumn, UpgradeColumn classPKColumn) {
62  
63          super("groupId");
64  
65          _classNameIdColumn = classNameIdColumn;
66          _classPKColumn = classPKColumn;
67      }
68  
69      public Object getNewValue(Object oldValue) throws Exception {
70          Long oldGroupId = (Long)oldValue;
71  
72          Long newGroupId = oldGroupId;
73  
74          if (oldGroupId.longValue() == 0) {
75              Long classNameId = (Long)_classNameIdColumn.getOldValue();
76              Long classPK = (Long)_classPKColumn.getOldValue();
77  
78              String className = PortalUtil.getClassName(classNameId.longValue());
79  
80              try {
81                  newGroupId = new Long(
82                      getGroupId(className, classPK.longValue()));
83              }
84              catch (PortalException pe) {
85                  throw new StagnantRowException(pe.getMessage(), pe);
86              }
87              catch (Exception e) {
88                  if (_log.isWarnEnabled()) {
89                      _log.warn(e, e);
90                  }
91              }
92          }
93  
94          return newGroupId;
95      }
96  
97      protected long getGroupId(String className, long classPK) throws Exception {
98          if (className.equals(BlogsEntry.class.getName())) {
99              BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
100 
101             return entry.getGroupId();
102         }
103         else if (className.equals(BookmarksEntry.class.getName())) {
104             BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(
105                 classPK);
106 
107             return entry.getFolder().getGroupId();
108         }
109         else if (className.equals(DLFileEntry.class.getName())) {
110             DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
111                 classPK);
112 
113             return fileEntry.getFolder().getGroupId();
114         }
115         else if (className.equals(IGImage.class.getName())) {
116             IGImage image = IGImageLocalServiceUtil.getImage(classPK);
117 
118             return image.getFolder().getGroupId();
119         }
120         else if (className.equals(JournalArticle.class.getName())) {
121             JournalArticleResource articleResource =
122                 JournalArticleResourceLocalServiceUtil.getArticleResource(
123                     classPK);
124 
125             return articleResource.getGroupId();
126         }
127         else if (className.equals(MBMessage.class.getName())) {
128             MBMessage message = MBMessageLocalServiceUtil.getMessage(classPK);
129 
130             return message.getCategory().getGroupId();
131         }
132         else if (className.equals(WikiPage.class.getName())) {
133             WikiPageResource pageResource =
134                 WikiPageResourceLocalServiceUtil.getPageResource(classPK);
135 
136             WikiNode node = WikiNodeLocalServiceUtil.getNode(
137                 pageResource.getNodeId());
138 
139             return node.getGroupId();
140         }
141         else {
142             return 0;
143         }
144     }
145 
146     private static Log _log =
147         LogFactoryUtil.getLog(TagsAssetGroupIdUpgradeColumnImpl.class);
148 
149     private UpgradeColumn _classNameIdColumn;
150     private UpgradeColumn _classPKColumn;
151 
152 }