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