Liferay从一些遗留数据库将数据迁移到Liferay表中

时间:2015-05-06 20:02:36

标签: liferay liferay-6.2

我希望将数据从其中一个遗留数据库迁移到Liferay表之一。我可以编写迁移脚本,但我想知道liferay的“Counter”服务是否会出现问题。

例如,我有旧的自定义用户表。我需要将此表中的用户移动到Liferay的User_表。我可以使用sql脚本来移动数据。我想知道主键会发生什么。据我所知,Liferay有反制服务来创建主键并跟踪当前的id。

因此,迁移时我需要做任何事情,以便在迁移后不会搞砸计数器。

3 个答案:

答案 0 :(得分:0)

除了柜台之外,还有更多问题。您应严格使用Liferay的API导入内容。

这个答案)相信他们现在可以应对所有问题。你不能。不要去那里。只需使用API​​,它将负责更新所有必需的依赖项。此外,API还可以显示您在导入数据时仍需要的其他数据。

任何更详细描述的人都会让你陷入灾难之中。我们已经看到人们在手动写入数据库超过6个月后发现了他们的灾难(在特定情况下遇到重复的主键,例如特别是你提到的计数器)。当您(或其他人)手动写入数据库时​​,任何可能的故障都可能导致您的门户网站从一秒钟停止到另一秒。

答案 1 :(得分:0)

您可以通过调用REST端点(通过{MODEL_NAME} ServiceImpl.java定义)从Pentaho Data Integration迁移旧数据库中的数据,该端点返回我想要插入数据的实体的计数器增量。你只需要尊重每个获得新id的实体使用相同的计数器。

答案 2 :(得分: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