Liferay 将数据从一些遗留数据库迁移到 Liferay table
Liferay migrating data into Liferay table from some legacy database
我希望将数据从旧数据库之一迁移到 Liferay tables 之一。我可以编写迁移脚本,但我想知道 liferay 的 "Counter" 服务是否会出现问题。
例如,我有旧版自定义用户 table。我需要将此 table 中的用户移动到 Liferay 的 User_ table。我可以使用 sql 脚本来移动数据。我想知道主键会发生什么。据我所知,Liferay 有柜台服务来创建主键并跟踪当前 ID。
因此,在迁移过程中,我需要做些什么才能使计数器在迁移后不会被弄乱。
问题不仅仅是计数器。您应该严格 使用 Liferay 的 API 来导入内容。
我可以说出几个潜在问题需要注意,但这可能会遗漏更多 - 然而它会让您(或阅读此答案的其他人)相信他们现在可以应对所有问题。你不能。不要去那里。只需使用 API ,它将负责更新所有必需的依赖项。此外,API 可以清楚地表明您还需要哪些其他数据以及导入的数据。
任何描述得更详细的人都会让你陷入灾难。我们已经看到有人在手动写入数据库后 6 个多月才发现他们的灾难(在特定情况下 运行 到重复的主键中,例如特别是你提到的计数器)。当您早已忘记您(或其他人)手动写入数据库时,任何可能的故障都可能使您的门户网站从一秒到另一秒崩溃。
您可以通过调用 REST 端点(通过 {MODEL_NAME}ServiceImpl.java 定义)使用 Pentaho 数据集成从遗留数据库迁移数据,returns 实体的计数器增量我想将数据插入到。您只需要尊重为每个获取新 ID 的实体使用相同的计数器。
您可以从遗留用户列表中循环,并使用此 class:
创建 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
我希望将数据从旧数据库之一迁移到 Liferay tables 之一。我可以编写迁移脚本,但我想知道 liferay 的 "Counter" 服务是否会出现问题。
例如,我有旧版自定义用户 table。我需要将此 table 中的用户移动到 Liferay 的 User_ table。我可以使用 sql 脚本来移动数据。我想知道主键会发生什么。据我所知,Liferay 有柜台服务来创建主键并跟踪当前 ID。
因此,在迁移过程中,我需要做些什么才能使计数器在迁移后不会被弄乱。
问题不仅仅是计数器。您应该严格 使用 Liferay 的 API 来导入内容。
我可以说出几个潜在问题需要注意,但这可能会遗漏更多 - 然而它会让您(或阅读此答案的其他人)相信他们现在可以应对所有问题。你不能。不要去那里。只需使用 API ,它将负责更新所有必需的依赖项。此外,API 可以清楚地表明您还需要哪些其他数据以及导入的数据。
任何描述得更详细的人都会让你陷入灾难。我们已经看到有人在手动写入数据库后 6 个多月才发现他们的灾难(在特定情况下 运行 到重复的主键中,例如特别是你提到的计数器)。当您早已忘记您(或其他人)手动写入数据库时,任何可能的故障都可能使您的门户网站从一秒到另一秒崩溃。
您可以通过调用 REST 端点(通过 {MODEL_NAME}ServiceImpl.java 定义)使用 Pentaho 数据集成从遗留数据库迁移数据,returns 实体的计数器增量我想将数据插入到。您只需要尊重为每个获取新 ID 的实体使用相同的计数器。
您可以从遗留用户列表中循环,并使用此 class:
创建 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