Saturday, May 30, 2015

test

http://rsjblogs.blogspot.in/p/liferay-portlet-development.html


JAVA
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
package com.liferay.dlupload;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.upload.FileItem;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;


public class Document extends MVCPortlet {

public void uploadURL(ActionRequest req, ActionResponse resp)
             throws PortalException,IOException
             {System.out.println("inside upload action");
             FileItemFactory factory = new DiskFileItemFactory();
            
             ServletFileUpload upload = new ServletFileUpload(factory);
            
             Properties properties = PortalUtil.getPortalProperties();
            
             /*Creating parent directory where all file will be uploaded*/
            
             File directory = NewFile(properties.getProperty("liferay.home") + "/home/Uploaded_File");
             if(!directory.exists()){
                 directory.mkdirs();
                 System.out.println("type******************************dir existed");
             }
             /*Parsing action request to generate http servletRequest*/
             HttpServletRequest httpRequest = PortalUtil.getHttpServletRequest(req);
             try
             {
                 List fields =upload.parseRequest(httpRequest);
                 Iterator<FileItem> it = fields.iterator();
                 While (it.hasNext());
                 {
                     FileItem fileItem = it.next();
                   
                     if (!fileItem.isFormField())
                     {
                         String fileName = fileItem.getFileName();
                         File file = new File(directory, fileName);
                         String newFileName=generateFilename(file.getName().split("\\.")[0]);
                         File tempFile = File(directory, newFileName+"."+file.getName().split("\\.")[1]);
                       
                       
                         /*check the condition where same file exsist in the parent folder*/
                       
                         boolean bool = file.renameTo(tempFile);
                         try
                         {
                           
                             /*save the file to server*/
                             if(!file.exists()){
                                 fileItem.write(file); //saving into folder
                             }else{
                                 System.out.println("#######################    file" +file.getName() +" exists   ");

                             }
                                     
                           
                         }
                         catch (Exception e)
                         {
                             System.err.println("File  " + file.getName() + "could not be Saved");
                         }
                     }
                 }
             }
             private void While(boolean hasNext) {
    // TODO Auto-generated method stub
   
}
            private java.io.File NewFile(String string) {
    // TODO Auto-generated method stub
    return null;
}
            private File File(File directory, String string) {
    // TODO Auto-generated method stub
    return null;
}
            public String generateFilename(String name)
             {
                 Calendar cal = Calendar.getInstance();
                 return(name+"_"+cal.get(Calendar.DAY_OF_MONTH)+(cal.get(Calendar.MONTH)+1)+cal.get(Calendar.YEAR)+cal.get(Calendar.HOUR_OF_DAY)+cal.get(Calendar.MINUTE)+cal.get(Calendar.SECOND));
             }
             }
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
<%@ include file="/view.jsp"%>
<portlet:actionURL name="uploadURL" var="uploadURL" />

<aui:form action=<%="uploadURL.toString()" %> "enctype="multipart/form-data">
<aui:input name="normal" type="text"/>
<label>upload</label>
<input type="file" name"fileupload[]" multiple>
<aui:button name="upload" tupe="submit">upload</aui:button>
</aui:form>

Friday, May 29, 2015

test

http://stackoverflow.com/questions/23452484/create-folder-and-upload-file-using-servlet
vivekliferayblogspote.blogspot.in/2013/12/liferay-61-multiple-image-upload.html
package com.upload.doc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.ProcessAction;

import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;



  public class DocUpload extends MVCPortlet {
      @ProcessAction(name = "addProduct")
      public void addProduct(ActionRequest request, ActionResponse response) {
              String name = ParamUtil.getString(request,"name");
                  String anotherTryForName = request.getParameter("name");
              System.out.println(name);
                  System.out.println(anotherTryForName);
              System.out.println(uploadImg(request));
      }
      private String uploadImg(ActionRequest request) {

              String realPath = getPortletContext().getRealPath("/");
             
              byte[] bytes = null;
              try {
                  UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
                  String sourceFileName =uploadRequest.getFileName("imgFile");
                /***********************************************
                File newFile=null;
              
                if ((bytes != null) && (bytes.length > 0)) {
               File file = new File("/home/neosoft/test",file);
                try {
                newFile = new File(realPath+"html/portlet/"+sourceFileName);
                FileInputStream fileInputStream = new FileInputStream(file);
                FileOutputStream fileOutputStream = new FileOutputStream(newFile);           
                fileInputStream.read(bytes);              
                fileOutputStream.write(bytes, 0, bytes.length);                  
                fileOutputStream.close();
                fileInputStream.close();
         }
                ************************************************/
                
                  File file = uploadRequest.getFile("imgFile");
                  try {
                      bytes = FileUtil.getBytes(file);
                  } catch (IOException e2) {
                      e2.printStackTrace();
                  }
                  File newFile = null;

                  if ((bytes != null) && (bytes.length > 0)) {

                      try {
                          newFile = new File(realPath + "demo/productimg/"+ sourceFileName);
                          FileInputStream fileInputStream = new FileInputStream(file);
                          FileOutputStream fileOutputStream = new FileOutputStream(newFile);
                          fileInputStream.read(bytes);
                          fileOutputStream.write(bytes, 0, bytes.length);
                          fileOutputStream.close();
                          fileInputStream.close();
                      } catch (FileNotFoundException e) {
                          System.out.println("File Not Found.");
                          e.printStackTrace();
                      } catch (IOException e1) {
                          System.out.println("Error Reading The File.");
                          e1.printStackTrace();
                      }
                  }
                  return sourceFileName;
              } catch (Exception e) {
                  System.out.println("Exception::::" + e.getMessage());
                  return "";
              }
      }    
     
     
}



****************************

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>

 <portlet:actionURL var='create' name="addProduct">
</portlet:actionURL>   
<form id="addform" action="<%=create%>" method="post" enctype="multipart/form-data">

    <dl class="dl-horizontal">
        <dt>Product Name</dt>
        <dd>
            <input type="text"  name="name"  />
        </dd>
        <dt>Image File</dt>
        <dd>
            <input type="file" name="imgFile" >
        </dd>
    </dl>
        <button type="submit">Save</button>
</form>

JSP


Monday, June 23, 2014

User Registration Portlet Customization

The registration or create account portlet in Liferay portal can be customized to add/remove some of its field without any coding.

This blog explain some of these customization properties which developers can add them in portal-ext to change the behavior of user registration portlet.

users.screen.name.always.autogenerate - If you do not want your users to provide screen name, you can use this property hide screen name text field. The default value is false. If it is changed to true, the screen name field will not appear on the registration page and screen name will be auto generated by the portal.

login.create.account.allow.custom.password - The default registration portlet in Liferay doesn't provide option of password selection to user. If this property is set to true, the registration screen will show password fields to user and user can provide password of his/her choice.

field.enable.com.liferay.portal.model.Contact.male - If your site is not interested in knowing the gender of the user, you can remove gender selection from the page by setting false value of this property.

field.enable.com.liferay.portal.model.Contact.birthday - Last configuration is for birth date field. You can remove bith date selection by setting false value to this property.

Following screen shot shows customized registration portlet using above properties.

captcha.check.portal.create_account - This propery can be used to tell whether or not to use captcha checks for the account creation.

There are few other properties related to user registration which requires more attaention.
users.email.address.required - Set this to false if you want to be able to create users without an email address. An email address will be automatically assigned to a user based on the property "users.email.address.auto.suffix". If user is entering his email id, the system will not generate a new one. Usually, setting this property to false will make sense when you don't want user to use email address for login.