1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class TagsAssetGroupIdUpgradeColumnImpl extends BaseUpgradeColumnImpl {
58  
59      public TagsAssetGroupIdUpgradeColumnImpl(
60          UpgradeColumn classNameIdColumn, UpgradeColumn classPKColumn) {
61  
62          super("groupId");
63  
64          _classNameIdColumn = classNameIdColumn;
65          _classPKColumn = classPKColumn;
66      }
67  
68      public Object getNewValue(Object oldValue) throws Exception {
69          Long oldGroupId = (Long)oldValue;
70  
71          Long newGroupId = oldGroupId;
72  
73          if (oldGroupId.longValue() == 0) {
74              Long classNameId = (Long)_classNameIdColumn.getOldValue();
75              Long classPK = (Long)_classPKColumn.getOldValue();
76  
77              String className = PortalUtil.getClassName(classNameId.longValue());
78  
79              try {
80                  newGroupId = new Long(
81                      getGroupId(className, classPK.longValue()));
82              }
83              catch (PortalException pe) {
84                  throw new StagnantRowException(pe.getMessage(), pe);
85              }
86              catch (Exception e) {
87                  if (_log.isWarnEnabled()) {
88                      _log.warn(e, e);
89                  }
90              }
91          }
92  
93          return newGroupId;
94      }
95  
96      protected long getGroupId(String className, long classPK) throws Exception {
97          if (className.equals(BlogsEntry.class.getName())) {
98              BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
99  
100             return entry.getGroupId();
101         }
102         else if (className.equals(BookmarksEntry.class.getName())) {
103             BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(
104                 classPK);
105 
106             return entry.getFolder().getGroupId();
107         }
108         else if (className.equals(DLFileEntry.class.getName())) {
109             DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
110                 classPK);
111 
112             return fileEntry.getFolder().getGroupId();
113         }
114         else if (className.equals(IGImage.class.getName())) {
115             IGImage image = IGImageLocalServiceUtil.getImage(classPK);
116 
117             return image.getFolder().getGroupId();
118         }
119         else if (className.equals(JournalArticle.class.getName())) {
120             JournalArticleResource articleResource =
121                 JournalArticleResourceLocalServiceUtil.getArticleResource(
122                     classPK);
123 
124             return articleResource.getGroupId();
125         }
126         else if (className.equals(MBMessage.class.getName())) {
127             MBMessage message = MBMessageLocalServiceUtil.getMessage(classPK);
128 
129             return message.getCategory().getGroupId();
130         }
131         else if (className.equals(WikiPage.class.getName())) {
132             WikiPageResource pageResource =
133                 WikiPageResourceLocalServiceUtil.getPageResource(classPK);
134 
135             WikiNode node = WikiNodeLocalServiceUtil.getNode(
136                 pageResource.getNodeId());
137 
138             return node.getGroupId();
139         }
140         else {
141             return 0;
142         }
143     }
144 
145     private static Log _log =
146         LogFactoryUtil.getLog(TagsAssetGroupIdUpgradeColumnImpl.class);
147 
148     private UpgradeColumn _classNameIdColumn;
149     private UpgradeColumn _classPKColumn;
150 
151 }