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