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.portlet.webform.util;
24  
25  import com.liferay.counter.service.CounterLocalServiceUtil;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.util.PropsKeys;
32  import com.liferay.portal.util.PropsUtil;
33  import com.liferay.portlet.expando.NoSuchTableException;
34  import com.liferay.portlet.expando.model.ExpandoColumnConstants;
35  import com.liferay.portlet.expando.model.ExpandoTable;
36  import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
37  import com.liferay.portlet.expando.service.ExpandoRowLocalServiceUtil;
38  import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
39  
40  import java.io.BufferedReader;
41  import java.io.IOException;
42  import java.io.StringReader;
43  
44  import java.util.ArrayList;
45  import java.util.List;
46  
47  import javax.portlet.PortletPreferences;
48  
49  /**
50   * <a href="WebFormUtil.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Daniel Weisser
53   * @author Jorge Ferrer
54   *
55   */
56  public class WebFormUtil {
57  
58      public static final int MAX_FIELDS = GetterUtil.getInteger(
59          PropsUtil.get(PropsKeys.WEB_FORM_PORTLET_MAX_FIELDS));
60  
61      public static ExpandoTable addTable(String tableName)
62          throws PortalException, SystemException {
63  
64          try {
65              ExpandoTableLocalServiceUtil.deleteTable(
66                  WebFormUtil.class.getName(), tableName);
67          }
68          catch (NoSuchTableException nste) {
69          }
70  
71          return ExpandoTableLocalServiceUtil.addTable(
72              WebFormUtil.class.getName(), tableName);
73      }
74  
75      public static ExpandoTable checkTable(
76              String tableName, PortletPreferences prefs)
77          throws Exception {
78  
79          ExpandoTable expandoTable = null;
80  
81          try {
82              expandoTable = ExpandoTableLocalServiceUtil.getTable(
83                  WebFormUtil.class.getName(), tableName);
84          }
85          catch (NoSuchTableException nste) {
86              expandoTable = addTable(tableName);
87  
88              int i = 1;
89  
90              String fieldLabel = prefs.getValue(
91                  "fieldLabel" + i, StringPool.BLANK);
92  
93              while ((i == 1) || (Validator.isNotNull(fieldLabel))) {
94                  ExpandoColumnLocalServiceUtil.addColumn(
95                      expandoTable.getTableId(), fieldLabel,
96                      ExpandoColumnConstants.STRING);
97  
98                  i++;
99  
100                 fieldLabel = prefs.getValue("fieldLabel" + i, StringPool.BLANK);
101             }
102         }
103 
104         return expandoTable;
105     }
106 
107     public static String getNewDatabaseTableName(String portletId)
108         throws SystemException {
109 
110         long formId = CounterLocalServiceUtil.increment(
111             WebFormUtil.class.getName());
112 
113         return portletId + StringPool.UNDERLINE + formId;
114     }
115 
116     public static int getTableRowsCount(String tableName)
117         throws SystemException {
118 
119         return ExpandoRowLocalServiceUtil.getRowsCount(
120             WebFormUtil.class.getName(), tableName);
121     }
122 
123     public static String[] split(String s) {
124         return split(s, StringPool.COMMA);
125     }
126 
127     public static String[] split(String s, String delimiter) {
128         if (s == null || delimiter == null) {
129             return new String[0];
130         }
131 
132         s = s.trim();
133 
134         if (!s.endsWith(delimiter)) {
135             StringBuilder sb = new StringBuilder();
136 
137             sb.append(s);
138             sb.append(delimiter);
139 
140             s = sb.toString();
141         }
142 
143         if (s.equals(delimiter)) {
144             return new String[0];
145         }
146 
147         List<String> nodeValues = new ArrayList<String>();
148 
149         if (delimiter.equals("\n") || delimiter.equals("\r")) {
150             try {
151                 BufferedReader br = new BufferedReader(new StringReader(s));
152 
153                 String line = null;
154 
155                 while ((line = br.readLine()) != null) {
156                     nodeValues.add(line);
157                 }
158 
159                 br.close();
160             }
161             catch (IOException ioe) {
162                 ioe.printStackTrace();
163             }
164         }
165         else {
166             int offset = 0;
167             int pos = s.indexOf(delimiter, offset);
168 
169             while (pos != -1) {
170                 nodeValues.add(new String(s.substring(offset, pos)));
171 
172                 offset = pos + delimiter.length();
173                 pos = s.indexOf(delimiter, offset);
174             }
175         }
176 
177         return nodeValues.toArray(new String[nodeValues.size()]);
178     }
179 
180 }