1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.upgrade.StagnantRowException;
30  
31  /**
32   * <a href="IdReplacer.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class IdReplacer {
37  
38      public static String replaceLongIds(
39              String s, String begin, ValueMapper valueMapper)
40          throws Exception {
41  
42          if ((s == null) || (begin == null) ||
43              (valueMapper == null) || (valueMapper.size() == 0)) {
44  
45              return s;
46          }
47  
48          char[] charArray = s.toCharArray();
49  
50          StringBuilder sb = new StringBuilder(s.length());
51  
52          int pos = 0;
53  
54          while (true) {
55              int x = s.indexOf(begin, pos);
56              int y = _getEndPos(charArray, x + begin.length());
57  
58              if ((x == -1) || (y == -1)) {
59                  sb.append(s.substring(pos, s.length()));
60  
61                  break;
62              }
63              else {
64                  sb.append(s.substring(pos, x + begin.length()));
65  
66                  String oldString = s.substring(x + begin.length(), y);
67  
68                  if (Validator.isNotNull(oldString)) {
69                      Long oldValue = new Long(GetterUtil.getLong(oldString));
70  
71                      Long newValue = null;
72  
73                      try {
74                          newValue = (Long)valueMapper.getNewValue(oldValue);
75                      }
76                      catch (StagnantRowException sre) {
77                          if (_log.isWarnEnabled()) {
78                              _log.warn(sre);
79                          }
80                      }
81  
82                      if (newValue == null) {
83                          newValue = oldValue;
84                      }
85  
86                      sb.append(newValue);
87                  }
88  
89                  pos = y;
90              }
91          }
92  
93          return sb.toString();
94      }
95  
96      public String replaceLongIds(
97              String s, String begin, String end, ValueMapper valueMapper)
98          throws Exception {
99  
100         if ((s == null) || (begin == null) || (end == null) ||
101             (valueMapper == null) || (valueMapper.size() == 0)) {
102 
103             return s;
104         }
105 
106         StringBuilder sb = new StringBuilder(s.length());
107 
108         int pos = 0;
109 
110         while (true) {
111             int x = s.indexOf(begin, pos);
112             int y = s.indexOf(end, x + begin.length());
113 
114             if ((x == -1) || (y == -1)) {
115                 sb.append(s.substring(pos, s.length()));
116 
117                 break;
118             }
119             else {
120                 sb.append(s.substring(pos, x + begin.length()));
121 
122                 Long oldValue = new Long(GetterUtil.getLong(
123                     s.substring(x + begin.length(), y)));
124 
125                 Long newValue = null;
126 
127                 try {
128                     newValue = (Long)valueMapper.getNewValue(oldValue);
129                 }
130                 catch (StagnantRowException sre) {
131                     if (_log.isWarnEnabled()) {
132                         _log.warn(sre);
133                     }
134                 }
135 
136                 if (newValue == null) {
137                     newValue = oldValue;
138                 }
139 
140                 sb.append(newValue);
141 
142                 pos = y;
143             }
144         }
145 
146         return sb.toString();
147     }
148 
149     private static int _getEndPos(char[] charArray, int pos) {
150         while (true) {
151             if (pos >= charArray.length) {
152                 break;
153             }
154 
155             if (!Character.isDigit(charArray[pos])) {
156                 break;
157             }
158 
159             pos++;
160         }
161 
162         return pos;
163     }
164 
165     private static Log _log = LogFactoryUtil.getLog(IdReplacer.class);
166 
167 }