liferay already have a default database "lportal". In this we
creating a external database on each click on submission .
Explanation:- create account page for registration purpose and in this one form will be there "company name" and some other form also be there , now by the click on submit button a external_database would be generated by the company name .
The following steps:-
Step1:create portlet "ShareDatabaseCreate".
Step2: In View.jsp
Step3:create service builder and build service's as below
Step4:create action class like below package com.shareddb;
com.jdbc.template
Note:-paste database template.java
paste table template.java
Step8: Deploy your project.
Step9:Run your project and check user_ tables .
Next blog : Single-page-application in liferay 6.2 !!
The following steps:-
Step1:create portlet "ShareDatabaseCreate".
Step2: In View.jsp
<%@page import="javax.portlet.ActionRequest"%>
<%@page import="javax.portlet.PortletURL"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="/WEB-INF/tld/aui.tld" prefix="aui" %>
<portlet:defineObjects />
<%
PortletURL actionURL=renderResponse.createActionURL();
actionURL.setParameter(ActionRequest.ACTION_NAME, "addUser");
%>
<aui:form action="<%=actionURL.toString() %>" method="post">
<aui:input name="firstName" label="First Name">
<aui:validator name="required" errorMessage="Sorry, firstname required."/>
</aui:input>
<aui:input name="lastName" label="Last Name">
<aui:validator name="required" errorMessage="Sorry, lastname required."/>
</aui:input>
<aui:input name="emailAddress" label="Email Address">
<aui:validator name="required" errorMessage="Sorry, email address required."/>
</aui:input>
<aui:input name="companyName" label="CompanyName">
<aui:validator name="required" errorMessage="Sorry, company name required."/>
</aui:input>
</aui:form>
<%@page import="javax.portlet.PortletURL"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="/WEB-INF/tld/aui.tld" prefix="aui" %>
<portlet:defineObjects />
<%
PortletURL actionURL=renderResponse.createActionURL();
actionURL.setParameter(ActionRequest.ACTION_NAME, "addUser");
%>
<aui:form action="<%=actionURL.toString() %>" method="post">
<aui:input name="firstName" label="First Name">
<aui:validator name="required" errorMessage="Sorry, firstname required."/>
</aui:input>
<aui:input name="lastName" label="Last Name">
<aui:validator name="required" errorMessage="Sorry, lastname required."/>
</aui:input>
<aui:input name="emailAddress" label="Email Address">
<aui:validator name="required" errorMessage="Sorry, email address required."/>
</aui:input>
<aui:input name="companyName" label="CompanyName">
<aui:validator name="required" errorMessage="Sorry, company name required."/>
</aui:input>
</aui:form>
Step3:create service builder and build service's as below
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd">
<service-builder package-path="com.database">
<author>Abhishek-kumar</author>
<namespace>shareddb</namespace>
<entity name="DatabaseInformation" local-service="true" remote-service="false">
<column name="id" type="long" id-type="increment" />
<column name="userId" type="long" />
<column name="registerCompanyName" type="String" primary="true"/>
<column name="isDatabaseExist" type="Boolean" />
<column name="createDate" type="Date" />
<column name="modifiedDate" type="Date" />
</entity>
</service-builder>
Step4:create action class like below package com.shareddb;
package com.shareddb;
import java.util.Date;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import com.database.model.DatabaseInformation;
import com.database.model.impl.DatabaseInformationImpl;
import com.database.service.DatabaseInformationLocalServiceUtil;
import com.database.service.NewTableLocalService;
import com.database.service.NewTableLocalServiceUtil;
import com.jdbctemplate.DatabaseTemplate;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.UserServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class ActionClass extends MVCPortlet {
DatabaseTemplate databaseTemplate;
public ActionClass() {
databaseTemplate=new DatabaseTemplate();
}
public void addUser(ActionRequest actionRequest,
ActionResponse actionResponse) {
String firstName=ParamUtil.getString(actionRequest, "firstName");
String lastName=ParamUtil.getString(actionRequest, "lastName");
String emailAddress=ParamUtil.getString(actionRequest, "emailAddress");
String companyName=ParamUtil.getString(actionRequest, "companyName");
ThemeDisplay themeDisplay=(ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
long groupIds[]=null;
long organizationIds[]=null;
long roleIds[]=null;
long userGroupIds[]=null;
boolean wantToSendEmail=Boolean.FALSE;
ServiceContext serviceContext=new ServiceContext();
User user=null;
try {
user=UserServiceUtil.addUser(themeDisplay.getCompanyId(), true, StringPool.BLANK, StringPool.BLANK, true, StringPool.BLANK, emailAddress, 0, StringPool.BLANK, LocaleUtil.getDefault(), firstName, StringPool.BLANK, lastName, 0, 0, true, 1, 1, 1970, "", groupIds, organizationIds, roleIds, userGroupIds,wantToSendEmail, serviceContext);
//UserLocalServiceUtil.addUser(0L, themeDisplay.getCompanyId(), true, StringPool.BLANK, StringPool.BLANK, true, StringPool.BLANK, emailAddress, 0L, StringPool.BLANK, LocaleUtil.getDefault(), firstName, StringPool.BLANK, lastName, 0, 0, Boolean.TRUE, 1, 1, 1986, StringPool.BLANK, groupIds, organizationIds, roleIds, userGroupIds, wantToSendEmail, serviceContext);
} catch (PortalException e) {
//e.printStackTrace();
} catch (SystemException e) {
//e.printStackTrace();
}
System.out.println(user.getUserId());
if(Validator.isNotNull(user)){
System.out.println("--come1--");
DatabaseInformation databaseInformation=null;
try {
databaseInformation=DatabaseInformationLocalServiceUtil.fetchDatabaseInformation(companyName);
} catch (SystemException e) {
e.printStackTrace();
}
System.out.println("--come2--");
if(Validator.isNull(databaseInformation)){
System.out.println("databaseInformation is null");
//if data is not available in the db
boolean isDatabaseCreated = databaseTemplate.isDatabaseGenerated(Long.toString(user.getUserId()),MYSQL_USERNAME,MYSQL_PASSWORD);
_log.info("database object created : " + isDatabaseCreated);
if(Validator.isNotNull(isDatabaseCreated)){
databaseInformation=new DatabaseInformationImpl();
databaseInformation.setUserId(user.getUserId());
databaseInformation.setRegisterCompanyName(companyName);
databaseInformation.setIsDatabaseExist(Boolean.TRUE);
databaseInformation.setCreateDate(new Date());
databaseInformation.setModifiedDate(new Date());
try {
databaseInformation=DatabaseInformationLocalServiceUtil.addDatabaseInformation(databaseInformation);
} catch (SystemException e) {
//e.printStackTrace();
}
if(Validator.isNotNull(databaseInformation)){
//do something here/coding for table creation.
//call table creation template.
}
}
}else{
//if database is available
}
}
}
private static final String MYSQL_USERNAME = "root";
private static final String MYSQL_PASSWORD = "root";
private Log _log = LogFactoryUtil.getLog(ActionClass.class);
}
Step5:Create JDBC Template as belowimport java.util.Date;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import com.database.model.DatabaseInformation;
import com.database.model.impl.DatabaseInformationImpl;
import com.database.service.DatabaseInformationLocalServiceUtil;
import com.database.service.NewTableLocalService;
import com.database.service.NewTableLocalServiceUtil;
import com.jdbctemplate.DatabaseTemplate;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.UserServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class ActionClass extends MVCPortlet {
DatabaseTemplate databaseTemplate;
public ActionClass() {
databaseTemplate=new DatabaseTemplate();
}
public void addUser(ActionRequest actionRequest,
ActionResponse actionResponse) {
String firstName=ParamUtil.getString(actionRequest, "firstName");
String lastName=ParamUtil.getString(actionRequest, "lastName");
String emailAddress=ParamUtil.getString(actionRequest, "emailAddress");
String companyName=ParamUtil.getString(actionRequest, "companyName");
ThemeDisplay themeDisplay=(ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
long groupIds[]=null;
long organizationIds[]=null;
long roleIds[]=null;
long userGroupIds[]=null;
boolean wantToSendEmail=Boolean.FALSE;
ServiceContext serviceContext=new ServiceContext();
User user=null;
try {
user=UserServiceUtil.addUser(themeDisplay.getCompanyId(), true, StringPool.BLANK, StringPool.BLANK, true, StringPool.BLANK, emailAddress, 0, StringPool.BLANK, LocaleUtil.getDefault(), firstName, StringPool.BLANK, lastName, 0, 0, true, 1, 1, 1970, "", groupIds, organizationIds, roleIds, userGroupIds,wantToSendEmail, serviceContext);
//UserLocalServiceUtil.addUser(0L, themeDisplay.getCompanyId(), true, StringPool.BLANK, StringPool.BLANK, true, StringPool.BLANK, emailAddress, 0L, StringPool.BLANK, LocaleUtil.getDefault(), firstName, StringPool.BLANK, lastName, 0, 0, Boolean.TRUE, 1, 1, 1986, StringPool.BLANK, groupIds, organizationIds, roleIds, userGroupIds, wantToSendEmail, serviceContext);
} catch (PortalException e) {
//e.printStackTrace();
} catch (SystemException e) {
//e.printStackTrace();
}
System.out.println(user.getUserId());
if(Validator.isNotNull(user)){
System.out.println("--come1--");
DatabaseInformation databaseInformation=null;
try {
databaseInformation=DatabaseInformationLocalServiceUtil.fetchDatabaseInformation(companyName);
} catch (SystemException e) {
e.printStackTrace();
}
System.out.println("--come2--");
if(Validator.isNull(databaseInformation)){
System.out.println("databaseInformation is null");
//if data is not available in the db
boolean isDatabaseCreated = databaseTemplate.isDatabaseGenerated(Long.toString(user.getUserId()),MYSQL_USERNAME,MYSQL_PASSWORD);
_log.info("database object created : " + isDatabaseCreated);
if(Validator.isNotNull(isDatabaseCreated)){
databaseInformation=new DatabaseInformationImpl();
databaseInformation.setUserId(user.getUserId());
databaseInformation.setRegisterCompanyName(companyName);
databaseInformation.setIsDatabaseExist(Boolean.TRUE);
databaseInformation.setCreateDate(new Date());
databaseInformation.setModifiedDate(new Date());
try {
databaseInformation=DatabaseInformationLocalServiceUtil.addDatabaseInformation(databaseInformation);
} catch (SystemException e) {
//e.printStackTrace();
}
if(Validator.isNotNull(databaseInformation)){
//do something here/coding for table creation.
//call table creation template.
}
}
}else{
//if database is available
}
}
}
private static final String MYSQL_USERNAME = "root";
private static final String MYSQL_PASSWORD = "root";
private Log _log = LogFactoryUtil.getLog(ActionClass.class);
}
com.jdbc.template
Note:-paste database template.java
paste table template.java
Step8: Deploy your project.
Step9:Run your project and check user_ tables .
Code_for_External_Database_Code
hope this helps you...
Next blog : Single-page-application in liferay 6.2 !!
No comments:
Post a Comment