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