1
22
23 package com.liferay.portal.upgrade.v4_4_0;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.PropertiesUtil;
27 import com.liferay.portal.kernel.util.SafeProperties;
28 import com.liferay.portal.model.Layout;
29 import com.liferay.portal.service.LayoutLocalServiceUtil;
30 import com.liferay.portal.spring.hibernate.HibernateUtil;
31 import com.liferay.portal.upgrade.UpgradeException;
32 import com.liferay.portal.upgrade.UpgradeProcess;
33 import com.liferay.util.dao.DataAccess;
34
35 import java.sql.Connection;
36 import java.sql.PreparedStatement;
37 import java.sql.ResultSet;
38
39 import java.util.Properties;
40
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44
50 public class UpgradeLayout extends UpgradeProcess {
51
52 public void upgrade() throws UpgradeException {
53 _log.info("Upgrading");
54
55 try {
56 doUpgrade();
57 }
58 catch (Exception e) {
59 throw new UpgradeException(e);
60 }
61 }
62
63 protected void doUpgrade() throws Exception {
64 Connection con = null;
65 PreparedStatement ps = null;
66 ResultSet rs = null;
67
68 try {
69 con = HibernateUtil.getConnection();
70
71 ps = con.prepareStatement(
72 "select plid, typeSettings from Layout where type_ = " +
73 "'link_to_layout'");
74
75 rs = ps.executeQuery();
76
77 while (rs.next()) {
78 long plid = rs.getLong("plid");
79 String typeSettings = rs.getString("typeSettings");
80
81 String newTypeSettings = upgradeTypeSettings(typeSettings);
82
83 ps = con.prepareStatement(
84 "update Layout set typeSettings = ? where plid = " +
85 plid);
86
87 ps.setString(1, newTypeSettings);
88
89 ps.executeUpdate();
90
91 ps.close();
92 }
93 }
94 finally {
95 DataAccess.cleanUp(con, ps, rs);
96 }
97 }
98
99 protected String upgradeTypeSettings(String typeSettings) throws Exception {
100 Properties props = new SafeProperties();
101
102 PropertiesUtil.load(props, typeSettings);
103
104 long linkToPlid = GetterUtil.getLong(props.getProperty("linkToPlid"));
105
106 if (linkToPlid > 0) {
107 Layout layout = LayoutLocalServiceUtil.getLayout(linkToPlid);
108
109 props.remove("linkToPlid");
110 props.put("groupId", String.valueOf(layout.getGroupId()));
111 props.put(
112 "privateLayout", String.valueOf(layout.isPrivateLayout()));
113 props.put("linkToLayoutId", String.valueOf(layout.getLayoutId()));
114 }
115
116 return PropertiesUtil.toString(props);
117 }
118
119 private static Log _log = LogFactory.getLog(UpgradeLayout.class);
120
121 }