1   /**
2    * Copyright (c) 2000-2008 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.upgrade.StagnantRowException;
27  import com.liferay.portal.upgrade.util.BaseUpgradeColumnImpl;
28  import com.liferay.portal.upgrade.util.UpgradeColumn;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portlet.blogs.model.BlogsEntry;
31  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
32  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
33  import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
34  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
35  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
36  import com.liferay.portlet.imagegallery.model.IGImage;
37  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
38  import com.liferay.portlet.journal.model.JournalArticle;
39  import com.liferay.portlet.journal.model.JournalArticleResource;
40  import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
41  import com.liferay.portlet.messageboards.model.MBMessage;
42  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
43  import com.liferay.portlet.wiki.model.WikiNode;
44  import com.liferay.portlet.wiki.model.WikiPage;
45  import com.liferay.portlet.wiki.model.WikiPageResource;
46  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
47  import com.liferay.portlet.wiki.service.WikiPageResourceLocalServiceUtil;
48  
49  import org.apache.commons.logging.Log;
50  import org.apache.commons.logging.LogFactory;
51  
52  /**
53   * <a href="TagsAssetGroupIdUpgradeColumnImpl.java.html"><b><i>View Source</i>
54   * </b></a>
55   *
56   * @author Brian Wing Shun Chan
57   *
58   */
59  public class TagsAssetGroupIdUpgradeColumnImpl extends BaseUpgradeColumnImpl {
60  
61      public TagsAssetGroupIdUpgradeColumnImpl(
62          UpgradeColumn classNameIdColumn, UpgradeColumn classPKColumn) {
63  
64          super("groupId");
65  
66          _classNameIdColumn = classNameIdColumn;
67          _classPKColumn = classPKColumn;
68      }
69  
70      public Object getNewValue(Object oldValue) throws Exception {
71          Long oldGroupId = (Long)oldValue;
72  
73          Long newGroupId = oldGroupId;
74  
75          if (oldGroupId.longValue() == 0) {
76              Long classNameId = (Long)_classNameIdColumn.getOldValue();
77              Long classPK = (Long)_classPKColumn.getOldValue();
78  
79              String className = PortalUtil.getClassName(classNameId.longValue());
80  
81              try {
82                  newGroupId = new Long(
83                      getGroupId(className, classPK.longValue()));
84              }
85              catch (PortalException pe) {
86                  throw new StagnantRowException(pe.getMessage(), pe);
87              }
88              catch (Exception e) {
89                  if (_log.isWarnEnabled()) {
90                      _log.warn(e, e);
91                  }
92              }
93          }
94  
95          return newGroupId;
96      }
97  
98      protected long getGroupId(String className, long classPK) throws Exception {
99          if (className.equals(BlogsEntry.class.getName())) {
100             BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
101 
102             return entry.getGroupId();
103         }
104         else if (className.equals(BookmarksEntry.class.getName())) {
105             BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(
106                 classPK);
107 
108             return entry.getFolder().getGroupId();
109         }
110         else if (className.equals(DLFileEntry.class.getName())) {
111             DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
112                 classPK);
113 
114             return fileEntry.getFolder().getGroupId();
115         }
116         else if (className.equals(IGImage.class.getName())) {
117             IGImage image = IGImageLocalServiceUtil.getImage(classPK);
118 
119             return image.getFolder().getGroupId();
120         }
121         else if (className.equals(JournalArticle.class.getName())) {
122             JournalArticleResource articleResource =
123                 JournalArticleResourceLocalServiceUtil.getArticleResource(
124                     classPK);
125 
126             return articleResource.getGroupId();
127         }
128         else if (className.equals(MBMessage.class.getName())) {
129             MBMessage message = MBMessageLocalServiceUtil.getMessage(classPK);
130 
131             return message.getCategory().getGroupId();
132         }
133         else if (className.equals(WikiPage.class.getName())) {
134             WikiPageResource pageResource =
135                 WikiPageResourceLocalServiceUtil.getPageResource(classPK);
136 
137             WikiNode node = WikiNodeLocalServiceUtil.getNode(
138                 pageResource.getNodeId());
139 
140             return node.getGroupId();
141         }
142         else {
143             return 0;
144         }
145     }
146 
147     private static Log _log =
148         LogFactory.getLog(TagsAssetGroupIdUpgradeColumnImpl.class);
149 
150     private UpgradeColumn _classNameIdColumn;
151     private UpgradeColumn _classPKColumn;
152 
153 }