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_3_0;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.ArrayUtil;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.model.Location;
31  import com.liferay.portal.model.Organization;
32  import com.liferay.portal.model.Role;
33  import com.liferay.portal.model.User;
34  import com.liferay.portal.model.UserGroup;
35  import com.liferay.portal.model.impl.ResourceImpl;
36  import com.liferay.portal.upgrade.UpgradeException;
37  import com.liferay.portal.upgrade.UpgradeProcess;
38  import com.liferay.portal.upgrade.util.DefaultUpgradeTableImpl;
39  import com.liferay.portal.upgrade.util.TempUpgradeColumnImpl;
40  import com.liferay.portal.upgrade.util.UpgradeColumn;
41  import com.liferay.portal.upgrade.util.UpgradeTable;
42  import com.liferay.portal.upgrade.v4_3_0.util.AvailableMappersUtil;
43  import com.liferay.portal.upgrade.v4_3_0.util.ClassPKContainer;
44  import com.liferay.portal.upgrade.v4_3_0.util.MBMessageIdMapper;
45  import com.liferay.portal.upgrade.v4_3_0.util.ResourceCodeIdUpgradeColumnImpl;
46  import com.liferay.portal.upgrade.v4_3_0.util.ResourcePrimKeyUpgradeColumnImpl;
47  import com.liferay.portal.util.PortalUtil;
48  import com.liferay.portlet.blogs.model.BlogsEntry;
49  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
50  import com.liferay.portlet.bookmarks.model.BookmarksFolder;
51  import com.liferay.portlet.calendar.model.CalEvent;
52  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
53  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
54  import com.liferay.portlet.documentlibrary.model.DLFolder;
55  import com.liferay.portlet.imagegallery.model.IGFolder;
56  import com.liferay.portlet.imagegallery.model.IGImage;
57  import com.liferay.portlet.journal.model.JournalArticle;
58  import com.liferay.portlet.journal.model.JournalStructure;
59  import com.liferay.portlet.journal.model.JournalTemplate;
60  import com.liferay.portlet.messageboards.model.MBCategory;
61  import com.liferay.portlet.messageboards.model.MBMessage;
62  import com.liferay.portlet.polls.model.PollsQuestion;
63  import com.liferay.portlet.shopping.model.ShoppingCategory;
64  import com.liferay.portlet.shopping.model.ShoppingItem;
65  import com.liferay.portlet.wiki.model.WikiNode;
66  import com.liferay.portlet.wiki.model.WikiPage;
67  
68  import java.sql.Types;
69  
70  import java.util.HashMap;
71  import java.util.Map;
72  
73  /**
74   * <a href="UpgradeResource.java.html"><b><i>View Source</i></b></a>
75   *
76   * @author Alexander Chow
77   * @author Brian Wing Shun Chan
78   *
79   */
80  public class UpgradeResource extends UpgradeProcess {
81  
82      public void upgrade() throws UpgradeException {
83          _log.info("Upgrading");
84  
85          try {
86              doUpgrade();
87          }
88          catch (Exception e) {
89              throw new UpgradeException(e);
90          }
91      }
92  
93      protected Map<Long, ClassPKContainer> getClassPKContainers() {
94          Map<Long, ClassPKContainer> classPKContainers =
95              new HashMap<Long, ClassPKContainer>();
96  
97          // BlogsEntry
98  
99          classPKContainers.put(
100             new Long(PortalUtil.getClassNameId(BlogsEntry.class.getName())),
101             new ClassPKContainer(
102                 AvailableMappersUtil.getBlogsEntryIdMapper(), true));
103 
104         // BookmarksEntry
105 
106         classPKContainers.put(
107             new Long(PortalUtil.getClassNameId(BookmarksEntry.class.getName())),
108             new ClassPKContainer(
109                 AvailableMappersUtil.getBookmarksEntryIdMapper(), true));
110 
111         // BookmarksFolder
112 
113         classPKContainers.put(
114             new Long(PortalUtil.getClassNameId(
115                 BookmarksFolder.class.getName())),
116             new ClassPKContainer(
117                 AvailableMappersUtil.getBookmarksFolderIdMapper(), true));
118 
119         // CalEvent
120 
121         classPKContainers.put(
122             new Long(PortalUtil.getClassNameId(CalEvent.class.getName())),
123             new ClassPKContainer(
124                 AvailableMappersUtil.getCalEventIdMapper(), true));
125 
126         // DLFileEntry
127 
128         classPKContainers.put(
129             new Long(PortalUtil.getClassNameId(DLFileEntry.class.getName())),
130             new ClassPKContainer(
131                 AvailableMappersUtil.getDLFileEntryIdMapper(), false));
132 
133         // DLFileShortcut
134 
135         classPKContainers.put(
136             new Long(PortalUtil.getClassNameId(DLFileShortcut.class.getName())),
137             new ClassPKContainer(
138                 AvailableMappersUtil.getDLFileShortcutIdMapper(), true));
139 
140         // DLFolder
141 
142         classPKContainers.put(
143             new Long(PortalUtil.getClassNameId(DLFolder.class.getName())),
144             new ClassPKContainer(
145                 AvailableMappersUtil.getDLFolderIdMapper(), true));
146 
147         // Group
148 
149         classPKContainers.put(
150             new Long(PortalUtil.getClassNameId(Group.class.getName())),
151             new ClassPKContainer(
152                 AvailableMappersUtil.getGroupIdMapper(), true));
153 
154         // IGFolder
155 
156         classPKContainers.put(
157             new Long(PortalUtil.getClassNameId(IGFolder.class.getName())),
158             new ClassPKContainer(
159                 AvailableMappersUtil.getIGFolderIdMapper(), true));
160 
161         // IGImage
162 
163         classPKContainers.put(
164             new Long(PortalUtil.getClassNameId(IGImage.class.getName())),
165             new ClassPKContainer(
166                 AvailableMappersUtil.getIGImageIdMapper(), false));
167 
168         // JournalArticle
169 
170         classPKContainers.put(
171             new Long(PortalUtil.getClassNameId(JournalArticle.class.getName())),
172             new ClassPKContainer(
173                 AvailableMappersUtil.getJournalArticleIdMapper(), false));
174 
175         // JournalStructure
176 
177         classPKContainers.put(
178             new Long(PortalUtil.getClassNameId(
179                 JournalStructure.class.getName())),
180             new ClassPKContainer(
181                 AvailableMappersUtil.getJournalStructureIdMapper(), false));
182 
183         // JournalTemplate
184 
185         classPKContainers.put(
186             new Long(PortalUtil.getClassNameId(
187                 JournalTemplate.class.getName())),
188             new ClassPKContainer(
189                 AvailableMappersUtil.getJournalTemplateIdMapper(), false));
190 
191         // Layout
192 
193         classPKContainers.put(
194             new Long(PortalUtil.getClassNameId(Layout.class.getName())),
195             new ClassPKContainer(
196                 AvailableMappersUtil.getLayoutPlidMapper(), false));
197 
198         // Location
199 
200         classPKContainers.put(
201             new Long(PortalUtil.getClassNameId(Location.class.getName())),
202             new ClassPKContainer(
203                 AvailableMappersUtil.getOrganizationIdMapper(), true));
204 
205         // MBCategory
206 
207         classPKContainers.put(
208             new Long(PortalUtil.getClassNameId(MBCategory.class.getName())),
209             new ClassPKContainer(
210                 AvailableMappersUtil.getMBCategoryIdMapper(), true));
211 
212         // MBMessage
213 
214         classPKContainers.put(
215             new Long(PortalUtil.getClassNameId(MBMessage.class.getName())),
216             new ClassPKContainer(
217                 new MBMessageIdMapper(
218                     AvailableMappersUtil.getMBMessageIdMapper()),
219                 false));
220 
221         // Organization
222 
223         classPKContainers.put(
224             new Long(PortalUtil.getClassNameId(Organization.class.getName())),
225             new ClassPKContainer(
226                 AvailableMappersUtil.getOrganizationIdMapper(), true));
227 
228         // PollsQuestion
229 
230         classPKContainers.put(
231             new Long(PortalUtil.getClassNameId(PollsQuestion.class.getName())),
232             new ClassPKContainer(
233                 AvailableMappersUtil.getPollsQuestionIdMapper(), true));
234 
235         // Role
236 
237         classPKContainers.put(
238             new Long(PortalUtil.getClassNameId(Role.class.getName())),
239             new ClassPKContainer(AvailableMappersUtil.getRoleIdMapper(), true));
240 
241         // ShoppingCategory
242 
243         classPKContainers.put(
244             new Long(PortalUtil.getClassNameId(
245                 ShoppingCategory.class.getName())),
246             new ClassPKContainer(
247                 AvailableMappersUtil.getShoppingCategoryIdMapper(), true));
248 
249         // ShoppingItem
250 
251         classPKContainers.put(
252             new Long(PortalUtil.getClassNameId(ShoppingItem.class.getName())),
253             new ClassPKContainer(
254                 AvailableMappersUtil.getShoppingItemIdMapper(), true));
255 
256         // User
257 
258         classPKContainers.put(
259             new Long(PortalUtil.getClassNameId(User.class.getName())),
260             new ClassPKContainer(
261                 AvailableMappersUtil.getUserIdMapper(), false));
262 
263         // UserGroup
264 
265         classPKContainers.put(
266             new Long(PortalUtil.getClassNameId(UserGroup.class.getName())),
267             new ClassPKContainer(
268                 AvailableMappersUtil.getUserGroupIdMapper(), true));
269 
270         // WikiNode
271 
272         classPKContainers.put(
273             new Long(PortalUtil.getClassNameId(WikiNode.class.getName())),
274             new ClassPKContainer(
275                 AvailableMappersUtil.getWikiNodeIdMapper(), true));
276 
277         // WikiPage
278 
279         classPKContainers.put(
280             new Long(PortalUtil.getClassNameId(WikiPage.class.getName())),
281             new ClassPKContainer(
282                 AvailableMappersUtil.getWikiPageIdMapper(), false));
283 
284         return classPKContainers;
285     }
286 
287     protected void doUpgrade() throws Exception {
288 
289         // Resource
290 
291         Object[][] resourceColumns1 = {
292             {"companyId", new Integer(Types.BIGINT)},
293             {"name", new Integer(Types.VARCHAR)},
294             {"scope", new Integer(Types.VARCHAR)}
295         };
296         Object[][] resourceColumns2 = ResourceImpl.TABLE_COLUMNS.clone();
297 
298         Object[][] resourceColumns = ArrayUtil.append(
299             resourceColumns1, resourceColumns2);
300 
301         UpgradeColumn companyIdColumn = new TempUpgradeColumnImpl("companyId");
302 
303         UpgradeColumn nameColumn = new TempUpgradeColumnImpl("name");
304 
305         UpgradeColumn scopeColumn = new TempUpgradeColumnImpl("scope");
306 
307         ResourceCodeIdUpgradeColumnImpl codeIdColumn =
308             new ResourceCodeIdUpgradeColumnImpl(
309                 companyIdColumn, nameColumn, scopeColumn);
310 
311         UpgradeColumn primKeyColumn = new ResourcePrimKeyUpgradeColumnImpl(
312             nameColumn, codeIdColumn, AvailableMappersUtil.getGroupIdMapper(),
313             getClassPKContainers(), AvailableMappersUtil.getLayoutPlidMapper());
314 
315         UpgradeTable upgradeTable = new DefaultUpgradeTableImpl(
316             ResourceImpl.TABLE_NAME, resourceColumns, companyIdColumn,
317             nameColumn, scopeColumn, codeIdColumn, primKeyColumn);
318 
319         String createSQL = ResourceImpl.TABLE_SQL_CREATE;
320 
321         createSQL =
322             createSQL.substring(0, createSQL.length() - 1) +
323                 ",companyId VARCHAR(75) null, name VARCHAR(75) null, " +
324                     "scope VARCHAR(75) null)";
325 
326         upgradeTable.setCreateSQL(createSQL);
327 
328         upgradeTable.updateTable();
329 
330         // Schema
331 
332         runSQL(_UPGRADE_SCHEMA);
333     }
334 
335     private static final String[] _UPGRADE_SCHEMA = {
336         "alter table Resource_ drop column companyId",
337         "alter table Resource_ drop column name",
338         "alter table Resource_ drop column scope"
339     };
340 
341     private static Log _log = LogFactoryUtil.getLog(UpgradeResource.class);
342 
343 }