1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.upgrade.v4_3_0.util;
16  
17  import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
18  import com.liferay.portal.kernel.upgrade.util.ValueMapper;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.StringUtil;
21  
22  /**
23   * <a href="ShoppingCartItemIdsUpgradeColumnImpl.java.html"><b><i>View Source
24   * </i></b></a>
25   *
26   * @author Brian Wing Shun Chan
27   */
28  public class ShoppingCartItemIdsUpgradeColumnImpl
29      extends BaseUpgradeColumnImpl {
30  
31      public ShoppingCartItemIdsUpgradeColumnImpl(
32          ValueMapper shoppingItemIdMapper) {
33  
34          super("itemIds");
35  
36          _shoppingItemIdMapper = shoppingItemIdMapper;
37      }
38  
39      public Object getNewValue(Object oldValue) throws Exception {
40          String[] itemIds = StringUtil.split((String)oldValue);
41  
42          for (int i = 0; i < itemIds.length; i++) {
43              String itemId = itemIds[i];
44  
45              int pos = itemId.indexOf("|");
46  
47              if (pos == -1) {
48                  itemIds[i] = String.valueOf(_shoppingItemIdMapper.getNewValue(
49                      new Long(GetterUtil.getLong(itemId))));
50              }
51              else {
52                  Long oldItemId = new Long(
53                      GetterUtil.getLong(itemId.substring(0, pos)));
54  
55                  itemIds[i] =
56                      _shoppingItemIdMapper.getNewValue(oldItemId) +
57                          itemId.substring(pos, itemId.length());
58              }
59          }
60  
61          return StringUtil.merge(itemIds);
62      }
63  
64      private ValueMapper _shoppingItemIdMapper;
65  
66  }