Friday, 19 December 2014

Adding image ,dispalying preview and storing it in to Database


 addCarCharges.jsp

<%@page import="com.slayer.service.ImageContentLocalServiceUtil"%>
<%@page import="com.slayer.model.ImageContent"%>
<%@page import="com.liferay.portal.model.Image"%>
<%@page import="com.liferay.portal.service.ImageLocalServiceUtil"%>
<%@page import="com.slayer.service.CarChargesLocalServiceUtil"%>
<%@page import="com.slayer.model.CarCharges"%>
<%@page import="javax.portlet.ActionRequest"%>
<%@page import="javax.portlet.PortletURL"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui"%>
<%@taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%>
<liferay-theme:defineObjects />
<portlet:defineObjects />
<head>
</head>
<style>
 .label{
vertical-align: middle;
 padding-top: 7%; 
}
/* form div {
height: 20px;
margin-top: 15px;
}   */
#buttn{
margin-left: 185px;
 width: 135px;

}
.w40{
 font-family: Helvetica,Arial,Verdana,sans-serif;
font-size: 12px;
text-align:right;
width: 20%;
float: left;
}
.w60{
font-size: 12px;

width: 80%;
float: left;
}
.textfield{
  width:130px;
height:18px;
padding-top: 5px;
  border-radius:4px;
  border:1px solid #649A32;
  box-shadow: 1px 1px 2px #649A32;
  float:left;
  margin-bottom:5px;
  margin-left:7px;
  }
 
</style>


 <div class="protlet-header border-bot">Add Car Charges</div>
<%
    PortletURL backUrl = renderResponse.createRenderURL();

    PortletURL addChargesUrl = renderResponse.createActionURL();
    addChargesUrl.setParameter(ActionRequest.ACTION_NAME,
            "addCarCharges");

    CarCharges car = null;

    String cartype = renderRequest.getParameter("type");
    String type = "", dailyRental = "", weeklyRental = "", dailyDamageWaiver = "", weeklyDamageWaiver = "";

   

    String imgPath = request.getContextPath() + "/html/Admin/cardetail/defaultCar.jpg";
    if (cartype != null) {
        try {
            car = CarChargesLocalServiceUtil.getCarCharges(cartype);
            type = car.getType();
            dailyRental = car.getDailyRental();
            weeklyRental = car.getWeeklyRental();
            dailyDamageWaiver = car.getWeeklyDamageWaiver();
            weeklyDamageWaiver = car.getDailyDamageWaiver();
            imgPath = themeDisplay.getPathImage() + "/image?img_id="+ car.getImageId();
        } catch (Exception e) {

        }
    }

   

   
%>

<!-- <div class="protlet-header border-bot">
Upload The Image Of Size 500*320 px(Resize Images Online For Better Quality)<br/>http://www.simpleimageresizer.com/upload
</div> -->

<script type="text/javascript">

    function PreviewImage()
    {   
   
        var img=document.getElementById("addimage").value;
        if(img==null && ""){
            alert("sgjkdfsd fsgf");
    }
        var oFReader = new FileReader();
        oFReader.readAsDataURL(document.getElementById("addimage").files[0]);

        oFReader.onload = function (oFREvent) {
            document.getElementById("uploadPreview").src = oFREvent.target.result;
        };
    }
   
    function formValidate()
    {
    if(document.getElementById("addimage").value=="")
    {
        alert("No Image Selected");
        return false;
    }
    }
</script>

<div>
<form action="<%=addChargesUrl.toString() %>" method="post" id="form" enctype="multipart/form-data">
   <div style="height:150px; ">
   
    <div class="label w40">
        <label>Add Image:</label>
    </div>
    <div class="w60">
        <img id="uploadPreview" src="<%=imgPath %>"
            style="width: 180px; height: 134px;" />
    </div>
    </div>
    <div class="w40 "><pre>  </pre></div>
<div class="w60">
        <input id="addimage" type="file" name="addimage"
            onchange="PreviewImage();" readonly="readonly" />
    </div>
   
<div class="w40"><label class="label">Type:</label></div>
<div class="w60">
<input class="textfield"
        data-msg-required="Please Enter The Type."
        data-rule-required="true"
        type="text" name="type"  id="type" value="<%=type%>"/></div>

<div class="w40"><label class="label">Daily Rental/Per Day:</label></div>
<div class="w60">
<input class="textfield"
       data-msg-required=" Enter Daily Rental/Per Day."
       data-rule-required="true"
       data-rule-number="true"
       type="text" name="dailyRental" id="dailyRental" value="<%=dailyRental%>"/></div>

<div class="w40"><label class="label">Weekly Rental/Per Day:</label></div>
<div class="w60">
<input  class="textfield"
       data-msg-required="Enter Weekly Rental/Per Day."
       data-rule-required="true"
       data-rule-number="true"
        type="text" name="weeklyRental" id="weeklyRental" value="<%=weeklyRental %>"/></div>
<div class="w40"><label class="label">Daily DamageWaiver/Per Day:</label></div>
<div class="w60">
<input  class="textfield"
        data-msg-required="Enter Daily DamageWaiver/Per Day."
        data-rule-required="true"
        data-rule-number="true"
        type="text" name="dailyDamageWaiver" id="dailyDamageWaiver" value="<%= dailyDamageWaiver%>"/></div>
<div class="w40"><label class="label">Weekly DamageWaiver/Per Day:</label></div>
<div class="w60">
<input   class="textfield"
        data-msg-required="Enter Weekly DamageWaiver/Per Day."
        data-rule-required="true"
        data-rule-number="true"
        type="text" name="weeklyDamageWaiver" id="weeklyDamageWaiver" value="<%=weeklyDamageWaiver %>"/></div>
<div class="w40"></div>
<div class="w60">
<input  id="buttn"  type="submit" value="Submit"/>
<a href="<%= backUrl%>" style="text-decoration: none; margin-left: 15px;"><input  type="button" value="Back" style=" width: 100px;"></a>
</div>
</form>
</div>






 Action class
CarDetailPortlet.java 


package com.test;

import java.io.File;
import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.model.Image;
import com.liferay.portal.model.User;
import com.liferay.portal.service.ImageLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.slayer.model.CarAvailability;
import com.slayer.model.CarCharges;
import com.slayer.model.CarDetails;
import com.slayer.model.ImageContent;
import com.slayer.model.impl.CarAvailabilityImpl;
import com.slayer.model.impl.CarChargesImpl;
import com.slayer.model.impl.CarDetailsImpl;
import com.slayer.model.impl.ImageContentImpl;
import com.slayer.service.CarAvailabilityLocalServiceUtil;
import com.slayer.service.CarChargesLocalServiceUtil;
import com.slayer.service.CarDetailsLocalServiceUtil;
import com.slayer.service.ImageContentLocalServiceUtil;

/**
 * Portlet implementation class CarDetailPortlet
 */
public class CarDetailPortlet extends MVCPortlet {
   

        public void addCarCharges(ActionRequest actionRequest, ActionResponse actionResponse)
                                      throws IOException, PortletException, SystemException
        {
            UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
           
            String type=ParamUtil.getString(uploadRequest, "type");
            String dailyRental=ParamUtil.getString(uploadRequest, "dailyRental");
            String weeklyRental=ParamUtil.getString(uploadRequest, "weeklyRental");
            String dailyDamageWaiver=ParamUtil.getString(uploadRequest, "dailyDamageWaiver");
            String weeklyDamageWaiver=ParamUtil.getString(uploadRequest, "weeklyDamageWaiver");
           
           
        File addimage = uploadRequest.getFile("addimage");

           
            CarCharges charge=null;
            try {
                charge = CarChargesLocalServiceUtil.getCarCharges(type);
               
            } catch (Exception e2) {
                // TODO Auto-generated catch block
                charge = new CarChargesImpl();
                Long imageId= CounterLocalServiceUtil.increment();
                charge.setImageId(imageId.toString());
            }
           
                                       
           
            Long id =Long.parseLong(charge.getImageId());
           
            charge.setType(type);
            charge.setDailyRental(dailyRental);
            charge.setWeeklyRental(weeklyRental);
            charge.setWeeklyDamageWaiver(weeklyDamageWaiver);
            charge.setDailyDamageWaiver(dailyDamageWaiver);
            charge.setWeeklyDamageWaiver(weeklyDamageWaiver);
            charge.setTotalCars(0);
            charge.setTotalBookedCars(0);
//            System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhh"+charge);
           
            try {
                CarChargesLocalServiceUtil.updateCarCharges(charge);   
                ImageLocalServiceUtil.updateImage(id, addimage);
            } catch (SystemException e) {
                e.printStackTrace();
           
            }catch(PortalException e1){
                e1.printStackTrace();
            }
               
       
               
    }


















 

No comments: