Creato da pastuweb.com
Share My Page
My Social Accounts
Account FaceBook Account LinkedIn Account Twitter Account Google Plus Account Git Hub
MyStackOverflowFavorites Portlet

MyStackOverflowFavorites View

 

This is the View JSP of my portlet: MyStackOverflowFavorites. You can find and dowload this portlet from Liferay Market.

You can show your favorites StackOverflow Questions-links in your websites.

 

 

 

 

 

 

 

 

 

 

 

 

 

MyStackOverflowFavorites Edit

 

This is the Edit JSP of MyStackOverflowFavorites.

In this form you can edit the default Favorite URL and StackOverflow Profile URL.

 

 

 

 

 

The project:

MyStackOverflowFavorites projectThe most important files are:

  • MyStackOverflowFavorites.java
  • liferay-display.xml
  • liferay-plugin-package.properties
  • liferay-portlet.xml
  • portlet.xml
  • edit.jsp
  • view.jsp

 

 

 

 

 

 

 

 

 

 

 

 

Now i show you the source code of every file:

liferay-display.xml

<?xml version="1.0"?>

<!DOCTYPE display PUBLIC "-//Liferay//DTD

Display 6.0.0//EN" "http://www.liferay.com/dtd/liferay-display_6_0_0.dtd">

 

<display>

<category name="Pasturenzi Portlets">

<portlet id="MyStackOverflowFavorites" />

</category>

</display>

 

liferay-plugin-package.properties

name=MyStackOverflowFavorites

module-group-id=liferay

module-incremental-version=1

tags=

short-description=Liferay 6.1

change-log=

page-url=http://www.appuntivari.net/informatica/programmazione/liferay/mystackoverflowfavorites-portlet-code

author=Pasturenzi Francesco

licenses=LGPL

liferay-versions=6.1.1+,6.1.20+

liferay-portlet.xml

<?xml version="1.0"?>

<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD

Portlet Application 6.0.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_6_0_0.dtd">

 

<liferay-portlet-app>

<portlet>

<portlet-name>MyStackOverflowFavorites</portlet-name>

<icon>/icon.png</icon>

<instanceable>true</instanceable>

<header-portlet-css>/css/main.css</header-portlet-css>

<footer-portlet-javascript>/js/main.js</footer-portlet-javascript>

 

<css-class-wrapper>MyStackOverflowFavorites-portlet</css-class-wrapper>

</portlet>

<role-mapper>

<role-name>administrator</role-name>

<role-link>Administrator</role-link>

</role-mapper>

<role-mapper>

<role-name>guest</role-name>

<role-link>Guest</role-link>

</role-mapper>

<role-mapper>

<role-name>power-user</role-name>

<role-link>Power User</role-link>

</role-mapper>

<role-mapper>

<role-name>user</role-name>

<role-link>User</role-link>

</role-mapper>

</liferay-portlet-app>

portlet.xml

<?xml version="1.0"?>

 

<portlet-app

version="2.0"

xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd

http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"

>

 

<portlet>

<portlet-name>MyStackOverflowFavorites</portlet-name>

<display-name>MyStackOverflowFavorites</display-name>

<portlet-class>com.appuntivarinet.MyStackOverflowFavorites</portlet-class>

<init-param>

<name>view-jsp</name>

<value>/view.jsp</value>

</init-param>

<init-param>

<name>edit-jsp</name>

<value>/edit.jsp</value>

</init-param>

<expiration-cache>0</expiration-cache>

<supports>

<mime-type>text/html</mime-type>

<portlet-mode>view</portlet-mode>

<portlet-mode>edit</portlet-mode>

</supports>

<portlet-info>

<title>MyStackOverflowFavorites</title>

<short-title>MyStackOverflowFavorites</short-title>

<keywords>MyStackOverflowFavorites</keywords>

</portlet-info>

<security-role-ref>

<role-name>administrator</role-name>

</security-role-ref>

<security-role-ref>

<role-name>guest</role-name>

</security-role-ref>

<security-role-ref>

<role-name>power-user</role-name>

</security-role-ref>

<security-role-ref>

<role-name>user</role-name>

</security-role-ref>

</portlet>

</portlet-app>

 

Now I'll show you the view.jsp (Portlet VIEW) and edit.jsp (Portlet Preferences):

view.jsp

<%

/**

 * Copyright (c) Pasturenzi Francesco

 * This is the VIEW of the Portlet.

 */

%>

<link type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"

rel="stylesheet" />

<link type="text/css" rel="stylesheet" href="<%=request.getContextPath()%>/css/styles-pajinate.css" />

 

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

<jsp:useBean id="urlFavorite" class="java.lang.String" scope="request" />

<jsp:useBean id="listaFavorite" class="java.lang.String" scope="request" />

<jsp:useBean id="urlProfile" class="java.lang.String" scope="request" />

 

<portlet:defineObjects />

<div style="width:450px;height:640px;">

<div id="wrapper" style="min-width:430px !important;padding:10px;margin:auto;position:relative;">

 

<div style="text-align:center;width:340px;margin:auto;">

<a href="#">

<img src="<%=request.getContextPath() %>/images/stackoverflow-logo.png"

style="width:300px;float:left;margin-top:-15px;" alt="StackOverflow logo" title="StackOverflow"/>

</a>

<a href="#">

<img src="<%=request.getContextPath() %>/images/pastuweb.png"

style="width:40px;float:left;margin-top:30px;" alt="Pastuweb logo" title="Pastuweb"/>

</a>

<span style="clear:left;"></span>

</div>

<div id="paging_container" class="container">

<div class="page_navigation"></div>

<br><br>

<p style="text-align:center;position:relative;padding-left:5px;border-bottom:1px solid #EFEFEF;font-weight:bold;">

Favorite Questions <span class="pastuweb_score">(Votes)</span>

</p>

<ul class="content">

<%=listaFavorite %>

</ul>

 

</div>

 

</div>

</div>

 

<script type="text/javascript">

AUI().use('get', function(A){

A.Get.script('http://code.jquery.com/jquery-1.9.1.js', {

        onSuccess: function(A){

       

        $("ul li").hover(

      function () {

      $(this).css("background","#EFEFEF");

      }, 

      function () {

      $(this).css("background","");

      }

    );

       

        }

});

 

});

 

AUI().use('get', function(A){

A.Get.script('<%=request.getContextPath()%>/js/jquery.pajinate.js', {

onSuccess: function(){

$('#paging_container').pajinate({

    num_page_links_to_display : 5,

    items_per_page : 7

    });

}

});

});

 

</script>

 

edit.jsp

<%

/**

 * Copyright (c) Pasturenzi Francesco

 * This is the form that you can see click on button "Preferences" of the Portlet

 */

%>

 

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

<jsp:useBean id="saveStackOverflowParametersURL" class="java.lang.String" scope="request" />

 

<portlet:defineObjects />

 

 

<form id="<portlet:namespace/>accountForm" action="<%=saveStackOverflowParametersURL%>" method="POST">

<span style="color:#000000;">URL Favorite Questions: <br>

<input type="text" name="inUrlFavorite" size="50"><br>

<span style="color:#000000;">URL StackOverflow Profile:</span><br>

<input type="text" name="inUrlProfile" size="50"><br>

 

<p style="text-align:right;">

<input type="submit" id="inviaUrlForm" title="Save" value="Save">

</p>

<br>

</form>

Now the most important file: MyStackOverflowFavorites.java
package com.appuntivarinet;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Logger;
 
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
 
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.liferay.portal.util.PortalUtil;
 
public class MyStackOverflowFavorites extends GenericPortlet {
 
/* initialize the default parameter of "portlet.xml" */
protected String editJSP;
protected String viewJSP;
 
public void init() throws PortletException{
editJSP = getInitParameter("edit-jsp");
viewJSP = getInitParameter("view-jsp");
}
 
 
//set the Portlet's default View
public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException{
PortletPreferences prefs = renderRequest.getPreferences();
 
String urlFavorite = (String) prefs.getValue("urlFavorite", "none");
String urlProfile = (String) prefs.getValue("urlProfile", "none");
 
/* Default value */
if(urlFavorite.equalsIgnoreCase("none")){
urlFavorite = "http://stackoverflow.com/users/2723164/pastuweb?tab=favorites";
}
if(urlProfile.equalsIgnoreCase("none")){
urlProfile = "http://stackoverflow.com/users/2723164/pastuweb";
}
 
 
String listaFavorite = getListaFavorite(urlFavorite);
 
 
renderRequest.setAttribute("urlFavorite", urlFavorite);
renderRequest.setAttribute("urlProfile", urlProfile);
renderRequest.setAttribute("listaFavorite", listaFavorite);
 
 
include(viewJSP, renderRequest, renderResponse);
}
 
 
/* special method: used to dispatch to right JSP */
protected void include(String path, RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException{
PortletRequestDispatcher portletRequestDispatcher = getPortletContext().getRequestDispatcher(path);
if(portletRequestDispatcher == null){
System.out.println("path : "+path+" non è valido.");
}else{
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}
 
/* set the Portlet's default Edit: it's a simple <form> */
public void doEdit(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException{
 
renderResponse.setContentType("text/html");
 
PortletURL saveStackOverflowParametersURL = renderResponse.createActionURL();
saveStackOverflowParametersURL.setParameter("saveStackOverflowParameters", "saveStackOverflowParameters");
renderRequest.setAttribute("saveStackOverflowParametersURL", saveStackOverflowParametersURL.toString());
 
/*You can add other ACTION URL for the EDIT JSP*/
 
include(editJSP, renderRequest, renderResponse);
 
}
 
 
/* ACTION call from Portlet's <form> of EDIT JSP */
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException{
 
String saveStackOverflowParameters = actionRequest.getParameter("saveStackOverflowParameters");
/*You can add other getParameter of EDIT JSP*/
 
if(saveStackOverflowParameters != null){
PortletPreferences prefs = actionRequest.getPreferences();
prefs.setValue("urlFavorite", actionRequest.getParameter("inUrlFavorite"));
prefs.setValue("urlProfile", actionRequest.getParameter("inUrlProfile"));
 
prefs.store();
actionResponse.setPortletMode(PortletMode.VIEW);
}
 
/*You can test other getParameter of EDIT JSP*/
 
}
 
 
/* Web Crawler per StackOveflow page */
public String getListaFavorite(String url)
throws FailingHttpStatusCodeException, MalformedURLException, IOException{
 
WebClient browser = new WebClient();
browser.setJavaScriptEnabled(false);
 
String val_meta_info = new String("");
String val_titolo = new String("");
String val_link = new String("");
String val_votes = new String("");
 
String content = new String("");
String temp = new String("");
 
 
HtmlPage page = browser.getPage(url);
 
System.out.println("lettura pagina");
HtmlElement favorite = page.getElementById("user-tab-favorites");
DomNodeList<HtmlElement> divs = favorite.getElementsByTagName("div");
 
for (HtmlElement div : divs) {
if(div.getAttribute("class").equals("user-tab-content")){
 
DomNodeList<HtmlElement> divs_user_tab_content = div.getElementsByTagName("div");
for (HtmlElement div_u_t_c : divs_user_tab_content) {
 
if(div_u_t_c.getAttribute("class").equals("user-questions")){
 
DomNodeList<HtmlElement> divs_user_questions= div_u_t_c.getElementsByTagName("div");
for (HtmlElement div_u_q : divs_user_questions) {
 
DomNodeList<HtmlElement> divs_q_summ= div_u_q.getElementsByTagName("div");
for (HtmlElement div_q_summ : divs_q_summ) {
 
if(div_q_summ.getAttribute("class").contains("question-counts")){
//Voti
HtmlElement votes = (HtmlElement)div_q_summ.getElementsByTagName("div").item(0);
val_votes = ((HtmlElement)votes.getElementsByTagName("div").item(0)).getTextContent().trim();
System.out.println("votes: "+ val_votes);
}
 
if(div_q_summ.getAttribute("class").contains("summary")){
 
//titolo
val_titolo =  ((HtmlElement)div_q_summ.getFirstChild()).getTextContent().trim();
System.out.println("titolo: "+val_titolo);
 
//link del titolo
val_link =  "http://stackoverflow.com"+((HtmlElement)((HtmlElement)div_q_summ.getFirstChild())
.getFirstChild()).getAttribute("href").toString();
System.out.println("link: "+val_link);
 
 
temp = "<li><p style=\"position:relative;\"><a href=\""+val_link+"\" target=\"_blank\"> "+
val_titolo+"</a>  <br> <span style=\"color:#FF8563;\"> ";
 
 
//meta-info
DomNodeList<HtmlElement> divs_meta_info= div_q_summ.getElementsByTagName("div");
for (HtmlElement item_meta_info : divs_meta_info) {
if(item_meta_info.getAttribute("class").contains("tags")){
 
val_meta_info = new String("");
DomNodeList<HtmlElement> meta_info= item_meta_info.getElementsByTagName("a");
for (HtmlElement a_meta_info : meta_info) {
if(a_meta_info.getAttribute("class").contains("post-tag")){
val_meta_info = val_meta_info+""+a_meta_info.getTextContent().trim()+",";
 
}
}
}
 
 
}
System.out.println("meta-info: "+val_meta_info.substring(0, val_meta_info.length()-1));
temp = temp + " "+val_meta_info.substring(0, val_meta_info.length()-1)+
"</span> <span class=\"pastuweb_score\"> "+val_votes+" </span></p></li>";
 
content = content + temp;
temp = new String("");
}
}
 
 
 
}
 
}
}
 
}
}
 
 
return content;
}
 
 
}
You can find this project on my GitHub Repository at this repository URL: https://github.com/pastuweb/liferay-my-stackoverflow-favorites
You can find the Hook project on my GitHub Repository at this repository URL: https://github.com/pastuweb/liferay-pasturenzi-hook
Media (1 Voto)
La media del punteggio è 5.0 stelle su 5.