2015-05-06 57 views
0

我正在尋找將數據從其中一箇舊數據庫遷移到Liferay表之一。我可以編寫遷移腳本,但我想知道是否會出現與Liferay的「Counter」服務有關的問題。Liferay將數據從一些舊數據庫遷移到Liferay表中

例如我有遺留的自定義用戶表。我需要將此表中的用戶移到Liferay的User_表中。我可以使用sql腳本來移動數據。我想知道主鍵發生了什麼。據我所知,Liferay已經提供了創建主鍵和跟蹤當前ID的服務。

所以,雖然遷移有什麼,我需要這樣做,計數器不會在遷移後混亂。

回答

0

還有更多的問題不只是櫃檯。您應該嚴格使用使用Liferay的API來導入內容。

幾個潛在問題的關注,但是這可能會錯過一些更多的 - 但它會成爲你(或其他人讀這個答案)相信,他們現在可以應付所有的問題。你不能。不要去那裏。只需使用API​​,它將負責更新所有必需的依賴關係。另外,該API明確了您還需要與導入的數據一起使用的其他數據。

任何描述更多細節的人都會爲你設置災難。我們已經看到,在人工寫入數據庫6個月後發現災難的人員(在特殊情況下會遇到重複的主鍵,例如您提到的計數器)。如果您已經忘記了您(或其他人)手動寫入數據庫,那麼任何可能的失敗都可能導致您的門戶從一秒鐘到另一秒失效。

+0

感謝您的回答。如果有多個表需要更新,你如何建議處理交易。如果我在LocalServiceImpl類中編寫代碼並調用所有其他實體,LocalServiceUtil類將處理事務,或者我應該使用Liferay的BatchSessionUtil –

0

通過調用REST端點(通過{MODEL_NAME} ServiceImpl.java定義),您可以使用Pentaho數據集成從舊數據庫中遷移數據,該端點返回要插入數據的實體的計數器增量。您只需要尊重每個使用新ID的實體使用相同的計數器。

0

你可以從傳統用戶的列表循環,也使用這個類來創建Liferay的用戶:

package com.yourcompany.yourapp.util; 

import java.util.Date; 
import java.util.List; 

import org.apache.log4j.Logger; 

import com.liferay.counter.service.CounterLocalServiceUtil; 
import com.liferay.portal.kernel.dao.orm.DynamicQuery; 
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil; 
import com.liferay.portal.kernel.dao.orm.OrderFactoryUtil; 
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil; 
import com.liferay.portal.kernel.exception.PortalException; 
import com.liferay.portal.kernel.exception.SystemException; 
import com.liferay.portal.kernel.uuid.PortalUUIDUtil; 
import com.liferay.portal.kernel.workflow.WorkflowConstants; 
import com.liferay.portal.model.Account; 
import com.liferay.portal.model.ClassName; 
import com.liferay.portal.model.Company; 
import com.liferay.portal.model.Contact; 
import com.liferay.portal.model.Group; 
import com.liferay.portal.model.LayoutSet; 
import com.liferay.portal.model.User; 
import com.liferay.portal.security.permission.PermissionChecker; 
import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil; 
import com.liferay.portal.security.permission.PermissionThreadLocal; 
import com.liferay.portal.service.AccountLocalServiceUtil; 
import com.liferay.portal.service.ClassNameLocalServiceUtil; 
import com.liferay.portal.service.CompanyLocalServiceUtil; 
import com.liferay.portal.service.ContactLocalServiceUtil; 
import com.liferay.portal.service.GroupLocalServiceUtil; 
import com.liferay.portal.service.LayoutSetLocalServiceUtil; 
import com.liferay.portal.service.RoleLocalServiceUtil; 
import com.liferay.portal.service.UserLocalServiceUtil; 
import com.liferay.portlet.asset.model.AssetEntry; 
import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil; 

public class UserUtil { 

    private static final Logger logger = Logger.getLogger(UserUtil.class); 

    private long companyId; 
    private long creatorUserId; 
    private long accountId; 
    private Date date; 

    public UserUtil() { 
     try { 
      DynamicQuery queryCompany = DynamicQueryFactoryUtil.forClass(Company.class) 
        .addOrder(OrderFactoryUtil.asc("companyId")); 
      List<Company> listCompany = (List<Company>) CompanyLocalServiceUtil.dynamicQuery(queryCompany, 0, 1); 
      companyId = listCompany.get(0).getCompanyId(); 
      //----------- 
      DynamicQuery queryAccount = DynamicQueryFactoryUtil.forClass(Account.class) 
        .addOrder(OrderFactoryUtil.asc("accountId")); 
      List<Account> listAccount = (List<Account>) AccountLocalServiceUtil.dynamicQuery(queryAccount, 0, 1); 
      accountId = listAccount.get(0).getAccountId(); 
      //----------- 
      DynamicQuery queryUser = DynamicQueryFactoryUtil.forClass(User.class) 
        .add(PropertyFactoryUtil.forName("defaultUser").eq(false)) 
        .addOrder(OrderFactoryUtil.asc("createDate")); 
      List<User> listUser = (List<User>) UserLocalServiceUtil.dynamicQuery(queryUser, 0, 1); 
      creatorUserId = listUser.get(0).getUserId(); 
      date = new Date(); 
     } catch (SystemException ex) { 
      logger.error(ex.getMessage()); 
     } 
    } 

    public void create(String screenName, String emailAddress, String hashedPassword, String fullName) { 
     try { 
      long contactId = CounterLocalServiceUtil.increment();//or use Contact.class.getName() as param 
      Contact contact = ContactLocalServiceUtil.createContact(contactId); 
      contact.setAccountId(accountId); 
      //contact.setBirthday(DateUtil.getDate("dd MM yyyy", "01 11 1986")); 
      contact.setCachedModel(true); 
      contact.setCompanyId(companyId); 
      contact.setCreateDate(date); 
      contact.setEmailAddress(emailAddress); 
      //contact.setEmployeeNumber(employeeNumber); 
      //contact.setEmployeeStatusId(employeeStatusId); 
      contact.setFirstName(fullName); 
      contact.setMale(true); 
      contact.setNew(true); 
      //contact.setUserId(creatorUserId); 
      User creatorUser = UserLocalServiceUtil.getUserById(creatorUserId); 
      contact.setUserName(creatorUser.getFullName()); 
      contact.setUserUuid(creatorUser.getUuid()); 
      ContactLocalServiceUtil.addContact(contact); 
      //---------------------- 
      long userId = CounterLocalServiceUtil.increment();//or use User.class.getName() as param 
      //---------------------- 
      User user = UserLocalServiceUtil.createUser(userId); 
      user.setAgreedToTermsOfUse(true); 
      user.setCachedModel(true); 
      user.setCompanyId(companyId); 
      user.setContactId(contactId); 
      user.setCreateDate(date); 
      user.setDefaultUser(false); 
      user.setDigest(null); 
      user.setEmailAddress(emailAddress); 
      user.setEmailAddressVerified(true); 
      user.setFirstName(fullName); 
      user.setGreeting("Hi " + user.getFirstName()); 
      user.setLanguageId("en_US"); 
      user.setModifiedDate(date); 
      user.setNew(true); 
      user.setPassword(hashedPassword); 
      user.setPasswordEncrypted(true); 
      user.setPasswordReset(false); 
      //user.setPasswordUnencrypted(); 
      user.setScreenName(screenName); 
      user.setStatus(WorkflowConstants.STATUS_APPROVED); 
      user.setTimeZoneId("UTC+7"); 
      user.setUserUuid(creatorUser.getUuid()); 
      user.setUuid(PortalUUIDUtil.generate()); 
      UserLocalServiceUtil.addUser(user); 
      //---------------------- 
      try { 
       // to avoid "PermissionChecker not Initialized" 
       PermissionChecker checker = PermissionCheckerFactoryUtil.create(creatorUser); 
       PermissionThreadLocal.setPermissionChecker(checker); 
      } catch (Exception e) { 
       logger.error(e.getMessage(), e); 
      } 
      //---------------------- 
      ClassName clsNameUser = ClassNameLocalServiceUtil.getClassName(Constants.USER_CLASS); 
      long classNameId = clsNameUser.getClassNameId(); 
      long groupId = CounterLocalServiceUtil.increment();// or use Group.class.getName() as param 
      Group group = GroupLocalServiceUtil.createGroup(groupId); 
      group.setActive(true); 
      group.setCachedModel(true); 
      group.setClassNameId(classNameId); 
      group.setClassPK(userId); 
      group.setCompanyId(companyId); 
      group.setCreatorUserId(creatorUser.getUserId()); 
      group.setCreatorUserUuid(creatorUser.getUuid()); 
      group.setFriendlyURL(String.valueOf(userId)); 
      group.setName(String.valueOf(userId)); 
      group.setNew(true); 
      group.setSite(false); 
      group.setTreePath("/" + groupId + "/"); 
      group.setType(0); 
      group.setUuid(PortalUUIDUtil.generate()); 
      GroupLocalServiceUtil.addGroup(group); 
      //----------------------------- 
      long layoutSetId1 = CounterLocalServiceUtil.increment();//or use LayoutSet.class.getName() as param 
      LayoutSet layoutSet1 = LayoutSetLocalServiceUtil.createLayoutSet(layoutSetId1); 
      layoutSet1.setCachedModel(true); 
      //layoutSet.setColorSchemeId(colorSchemeId); 
      layoutSet1.setCompanyId(companyId); 
      layoutSet1.setCreateDate(date); 
      //layoutSet.setCss(css); 
      layoutSet1.setGroupId(groupId); 
      //layoutSet.setLogo(logo); 
      //layoutSet.setLogoId(logoId); 
      layoutSet1.setModifiedDate(date); 
      layoutSet1.setNew(true); 
      layoutSet1.setPrivateLayout(true); 
      //layoutSet.setThemeId(themeId); 
      LayoutSetLocalServiceUtil.addLayoutSet(layoutSet1); 
      //----------------------------- 
      long layoutSetId2 = CounterLocalServiceUtil.increment();// or use LayoutSet.class.getName() as param 
      LayoutSet layoutSet2 = LayoutSetLocalServiceUtil.getLayoutSet(layoutSetId1); 
      layoutSet2.setLayoutSetId(layoutSetId2); 
      layoutSet2.setPrivateLayout(false); 
      LayoutSetLocalServiceUtil.addLayoutSet(layoutSet2); 
      //----------------------------- 
      long assetEntryId = CounterLocalServiceUtil.increment();//or use AssetEntry.class.getName() as param 
      AssetEntry assetEntry = AssetEntryLocalServiceUtil.createAssetEntry(assetEntryId); 
      assetEntry.setCompanyId(companyId); 
      assetEntry.setClassPK(userId); 
      assetEntry.setGroupId(groupId); 
      assetEntry.setClassNameId(classNameId); 
      //ae.setTitle(title); 
      assetEntry.setUserId(userId); 
      AssetEntryLocalServiceUtil.addAssetEntry(assetEntry); 
      //-------------------------------------------------- 
      //long orgAdminRoleId = RoleLocalServiceUtil.getRole(companyId, Constants.ORG_ADMIN_ROLE_NAME).getRoleId(); 
      //UserGroupRoleLocalServiceUtil.addUserGroupRoles(userId, groupId, new long[] { orgAdminRoleId }); 
      long orgUserRoleId = RoleLocalServiceUtil.getRole(companyId, Constants.ORG_USER_ROLE_NAME).getRoleId(); 
      RoleLocalServiceUtil.addUserRole(userId, orgUserRoleId); 
      long siteMemberRoleId = RoleLocalServiceUtil.getRole(companyId, Constants.SITE_MEMBER_ROLE_NAME).getRoleId(); 
      RoleLocalServiceUtil.addUserRole(userId, siteMemberRoleId); 
      //----------------------------------------------------------- 
     } catch (SystemException | PortalException ex) { 
      logger.error(ex.getMessage(), ex); 
     } 
    } 
} 

,那麼你可以創建UserUtil和呼叫方法create()舊版的用戶列表中環內新實例,是這樣的:

UserUtil userUtil = new UserUtil(); 
for (LegacyUser user : listOfLegacyUser) { 
    userUtil.create(........); 
} 

注意hashedPassword取決於你的哈希方法,在portal-ext.properties定義,默認值是: passwords.ecnryption.algorithm=PBKDF2WithHmacSHA1/160/128000 但您可以使用以下值之一:

passwords.encryption.algorithm=BCRYPT/10 
passwords.encryption.algorithm=MD2 
passwords.encryption.algorithm=MD5 
passwords.encryption.algorithm=NONE 
passwords.encryption.algorithm=PBKDF2WithHmacSHA1/160/128000 
passwords.encryption.algorithm=SHA 
passwords.encryption.algorithm=SHA-256 
passwords.encryption.algorithm=SHA-384 
passwords.encryption.algorithm=SSHA 
passwords.encryption.algorithm=UFC-CRYPT 
相關問題