参考:http://liferaytrends.blogspot.com/2013/01/tabs-with-search-container-in-liferay.html
想要的效果: 类似liferay 控制面板 搜索User 的页面
可以搜索, 分页, 显示提示信息
1.
liferay-util:buffer 标签可以把这个标签里面的标签内容当做一个变量赋值给其他标签
<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>
<liferay-ui:search-container-column-text
name="name"
buffer="buffer"
>
<liferay-util:buffer var="keywords">
<liferay-ui:icon-help message="it is help msg." />
</liferay-util:buffer>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%> <%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <%@ taglib prefix="liferay-ui" uri="http://liferay.com/tld/ui" %> <%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %> <%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%> <%@page import="com.liferay.portal.theme.ThemeDisplay"%> <%@page import="com.liferay.portal.kernel.util.WebKeys"%> <%@page import="com.labimo.lesson.portlet.LessonShareMode"%> <%@page import="com.labimo.lesson.*"%> <%@page import="com.labimo.lesson.service.*"%> <%@page import="com.liferay.portal.kernel.util.*"%> <%@page import="com.liferay.portal.model.*"%> <%@page import="java.util.*"%> <%@page import="com.labimo.lesson.model.*"%> <portlet:defineObjects /> <liferay-theme:defineObjects /> <portlet:actionURL var="editLessonNamePage" name="editLessonNamePage"> </portlet:actionURL> <portlet:actionURL var="setPasswordPage" name="setPasswordPage"> </portlet:actionURL> <portlet:actionURL var="deleteLesson" name="deleteLesson"> </portlet:actionURL> <portlet:actionURL var="setShareModePage" name="setShareModePage"> </portlet:actionURL> <portlet:actionURL var="replaceLessonPage" name="replaceLessonPage"> </portlet:actionURL> <portlet:actionURL var="uploadLessonPage" name="uploadLessonPage"> </portlet:actionURL> <portlet:actionURL var="searchLesson" name="searchLesson"> </portlet:actionURL> <%-- <aui:form action="<%= uploadLessonPage %>" method="post" name="<portlet:namespace />uploadLessonPage" cssClass="pull-left" > <aui:button name="uploadLessonPage" type="submit" value="Upload Lesson" /> </aui:form> <aui:form action="<%= searchLesson %>" method="post" name="<portlet:namespace />searchLesson" cssClass="pull-right"> <aui:row > <aui:input name="search" value="" label="" placeholder="Searech Lesson"></aui:input> <aui:button name="uploadLessonPage" type="submit" value="Search Lesson" cssClass="hidden"/> </aui:row > </aui:form> --%> <aui:nav-bar> <aui:nav> <aui:form action="<%= uploadLessonPage %>" method="post" name="<portlet:namespace />uploadLessonPage" cssClass="pull-left" > <aui:button name="uploadLessonPage" type="submit" value="Upload Lesson" /> </aui:form> </aui:nav> <aui:nav cssClass="pull-right"> <aui:form action="<%= searchLesson %>" method="post" name="<portlet:namespace />searchLesson" cssClass="pull-right form-search"> <%-- <input name="<portlet:namespace/>search" value="" placeholder="Searech Lesson"/> --%> <%-- <aui:input name="search" value="" label="" placeholder="Searech Lesson"></aui:input> <aui:button name="uploadLessonPage" type="submit" value="Search Lesson" cssClass="hidden"/>--%> <div id="" class="input-append"> <input type="text" class="search-query span9" name="<portlet:namespace/>search" value="" placeholder="Searech Lesson" title="" id="<portlet:namespace/>search"> <button class="btn" type="submit"> Search </button> </div> </aui:form> </aui:nav> </aui:nav-bar> <%-- <liferay-ui:search-form> <% UserSearch searchContainer = (UserSearch)request.getAttribute("liferay-ui:search:searchContainer"); UserDisplayTerms displayTerms = (UserDisplayTerms)searchContainer.getDisplayTerms(); %> <liferay-ui:search-toggle autoFocus="<%= windowState.equals(WindowState.MAXIMIZED) %>" buttonLabel="search" displayTerms="<%= displayTerms %>" id="toggle_id_users_admin_user_search" > <aui:fieldset> <%@ include file="/html/portlet/directory/user_search_user_name.jspf" %> <aui:input name="<%= displayTerms.SCREEN_NAME %>" size="20" value="<%= displayTerms.getScreenName() %>" /> <aui:input name="<%= displayTerms.EMAIL_ADDRESS %>" size="20" value="<%= displayTerms.getEmailAddress() %>" /> <c:if test="<%= showActiveUserSelect %>"> <aui:select name="<%= displayTerms.STATUS %>"> <aui:option label="any-status" selected="<%= (displayTerms.getStatus() == WorkflowConstants.STATUS_ANY) %>" value="<%= WorkflowConstants.STATUS_ANY %>" /> <aui:option label="active" selected="<%= (displayTerms.getStatus() == WorkflowConstants.STATUS_APPROVED) %>" value="<%= WorkflowConstants.STATUS_APPROVED %>" /> <aui:option label="inactive" selected="<%= (displayTerms.getStatus() == WorkflowConstants.STATUS_INACTIVE) %>" value="<%= WorkflowConstants.STATUS_INACTIVE %>" /> </aui:select> </c:if> </aui:fieldset> </liferay-ui:search-toggle> </liferay-ui:search-form> --%> <%-- <% PortletPreferences prefs = renderRequest.getPreferences(); %> --%> <%--<liferay-ui:search-container delta='5' emptyResultsMessage="No lesson , please upload lesson."> <liferay-ui:search-container-results results="${lessonList}" total="${fn:length(lessonList)}" /> --%> <%-- <% List<Lesson> lessonList2 = LessonLocalServiceUtil.findByNameOrKeywordsOrDescriptionAndUserId("", "", "", 202, 0, 5) ; %> --%> <liferay-ui:search-container searchContainer="${searchContainer}" > <%-- <liferay-ui:search-form page="<%= searchLesson %>" searchContainer="${searchContainer}" showAddButton="true" > <liferay-ui:search-toggle buttonLabel="search" displayTerms="${searchContainer.displayTerms}" id="toggle_id_users_admin_user_search" > <aui:fieldset> <aui:input name="search" size="20" value="search" /> </aui:fieldset> </liferay-ui:search-toggle> </liferay-ui:search-form> --%> <liferay-ui:search-container-results results="${searchcontainer.results}" total="${searchcontainer.total}"/> <liferay-ui:search-container-row className="com.labimo.lesson.model.Lesson" keyProperty="id" modelVar="lesson" > <% String editLessonNameUrl= editLessonNamePage + "&" +renderResponse.getNamespace()+"id="+ lesson.getRandomString() + lesson.getId(); String setPasswordPageUrl= setPasswordPage + "&" +renderResponse.getNamespace()+"id="+ lesson.getRandomString() + lesson.getId(); String deleteLessonUrl= deleteLesson + "&" +renderResponse.getNamespace()+"id="+ lesson.getRandomString() + lesson.getId(); String setShareModeUrl= setShareModePage + "&" +renderResponse.getNamespace()+"id="+ lesson.getRandomString() + lesson.getId(); String replaceLessonPageUrl= replaceLessonPage + "&" +renderResponse.getNamespace()+"id="+ lesson.getRandomString() + lesson.getId(); //ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); String lessonLink = "http://" + request.getServerName()+"/" +"lesson"+"/ls/"+ lesson.getRandomString() + lesson.getId() +"/player.html"; //String lessonLink = "http://" + request.getServerName()+"/" +"lesson"+"/ls?id="+ lesson.getRandomString() + lesson.getId(); String shareMode = LessonShareMode.values()[lesson.getShareMode()].name(); //lessonLink="<a href='http://www.google.com'>click here</a>"; %> <liferay-ui:search-container-column-text name="name" buffer="buffer" > <liferay-util:buffer var="keywords"> <liferay-ui:icon-help message="${lesson.keywords}" /> </liferay-util:buffer> <liferay-util:buffer var="description"> <liferay-ui:icon-help message="${lesson.description}" /> </liferay-util:buffer> <liferay-util:buffer var="edit"> <liferay-ui:icon image="edit" message="RENAME" /> </liferay-util:buffer> <liferay-util:buffer var="add"> <aui:fieldset helpMessage="add" label="keywords"> </aui:fieldset> </liferay-util:buffer> <% buffer.append(lesson.getName()); buffer.append(keywords); buffer.append(description); //buffer.append(edit); // buffer.append(add); %> </liferay-ui:search-container-column-text> <liferay-ui:search-container-column-text name="lessonLink" value="<%=lessonLink %>" href="<%=lessonLink %>" > </liferay-ui:search-container-column-text> <%-- <liferay-ui:icon-help message="123"></liferay-ui:icon-help> --%> <liferay-ui:search-container-column-text name="share" value="<%=shareMode %>" /> <fmt:formatDate value="${lesson.updateDate}" pattern="yyyy-MM-dd HH:mm:ss" var="updateDate"/> <liferay-ui:search-container-column-text name="updateDate" value="${updateDate}" /> <liferay-ui:search-container-column-text > <liferay-ui:icon-menu> <liferay-ui:icon image="edit" message="EDIT" url="<%=editLessonNameUrl %>" /> <liferay-ui:icon image="edit" message="SHARE" url="<%=setShareModeUrl %>" /> <liferay-ui:icon image="add" message="PASSWORD" url="<%=setPasswordPageUrl %>" /> <liferay-ui:icon image="edit" message="REPLACE" url="<%=replaceLessonPageUrl %>" /> <liferay-ui:icon image="delete" message="DELETE" url="<%=deleteLessonUrl %>" /> </liferay-ui:icon-menu> </liferay-ui:search-container-column-text> <%-- <liferay-ui:search-container-column-jsp path="/license_action.jsp" /> <c:set var="licenseUuid" scope="request" value="<%=license.getLicenseUuid() %>" /> --%> </liferay-ui:search-container-row> <liferay-ui:search-iterator /> </liferay-ui:search-container>
2. 把搜索的关键字设置到PortletURL 上面,这样在点击下一页或者前一页的时候, 搜索关键字这个参数的值不会丢失。
Map<String,String> paramMap= new HashMap<String,String>(); paramMap.put("search", search); /* * Creating IteratorURL and in that we will pass tab parameter */ PortletURL iteratorURL= renderResponse.createRenderURL(); Iterator<Map.Entry<String, String>> entries = paramMap.entrySet().iterator(); while (entries.hasNext()) { Map.Entry<String, String> entry = entries.next(); iteratorURL.setParameter(entry.getKey(), entry.getValue()); }
package com.labimo.lesson.portlet; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletConfig; import javax.portlet.PortletException; import javax.portlet.PortletURL; import javax.portlet.ProcessAction; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.RandomStringUtils; import org.apache.log4j.Logger; import com.google.gson.Gson; import com.labimo.lesson.model.Lesson; import com.labimo.lesson.model.impl.LessonImpl; import com.labimo.lesson.service.LessonLocalServiceUtil; import com.labimo.lesson.service.persistence.LessonUserUtil; import com.labimo.lesson.service.persistence.LessonUtil; import com.labimo.lesson.servlet.LaunchLessonServlet; import com.liferay.portal.kernel.dao.search.SearchContainer; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.language.LanguageUtil; import com.liferay.portal.kernel.servlet.SessionErrors; import com.liferay.portal.kernel.servlet.SessionMessages; import com.liferay.portal.kernel.upload.UploadPortletRequest; import com.liferay.portal.kernel.util.FileUtil; import com.liferay.portal.kernel.util.JavaConstants; import com.liferay.portal.kernel.util.ListUtil; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.portal.kernel.util.Validator; import com.liferay.portal.kernel.util.WebKeys; import com.liferay.portal.model.Group; import com.liferay.portal.model.User; import com.liferay.portal.model.UserGroup; import com.liferay.portal.service.GroupLocalServiceUtil; import com.liferay.portal.service.UserGroupLocalServiceUtil; import com.liferay.portal.service.UserLocalServiceUtil; import com.liferay.portal.theme.ThemeDisplay; import com.liferay.portal.util.PortalUtil; import com.liferay.util.bridges.mvc.MVCPortlet; public class LessonPortlet extends MVCPortlet { private static Logger logger = Logger.getLogger(LessonPortlet.class); @Override public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { String page = (String) renderRequest.getAttribute("page"); logger.info(" page =" + page); if(Validator.isNotNull(page)){ include(page, renderRequest, renderResponse); }else{ try { // List<Lesson> lessonList = LessonLocalServiceUtil.findByOwnerId(userId); // List<Lesson> lessonList = LessonLocalServiceUtil.findByNameOrKeywordsOrDescriptionAndUserId("", "", "", 202, 0, 5); // renderRequest.setAttribute("lessonList", lessonList); searchContainerData(renderRequest,renderResponse); } catch (Exception e) { e.printStackTrace(); } super.doView(renderRequest, renderResponse); } } @ProcessAction(name = "editLessonNamePage") public void editLessonNamePage(ActionRequest actionRequest, ActionResponse actionRresponse) { try { logger.info(" page = editLessonNamePage" ); User user = PortalUtil.getUser(actionRequest); String lessonIdAndUuid = ParamUtil.getString(actionRequest, "id"); logger.info(" lessonIdAndUuid = " +lessonIdAndUuid); if(Validator.isNotNull(user) && Validator.isNotNull(lessonIdAndUuid)){ long lessonId = Long.valueOf(lessonIdAndUuid.substring(6)); Lesson lesson = LessonLocalServiceUtil.findByIdAndOwnerId(lessonId, user.getUserId()); if(Validator.isNotNull(lesson)){ actionRequest.setAttribute("page", "/editLessonName.jsp"); actionRequest.setAttribute("lesson" , lesson); } } } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); SessionMessages.add(actionRequest, "error"); } } @ProcessAction(name = "editLessonName") public void editLessonName(ActionRequest actionRequest, ActionResponse actionRresponse) { logger.info("editLessonName"); try { User user = PortalUtil.getUser(actionRequest); String lessonIdAndUuid = ParamUtil.getString(actionRequest, "id"); String lessonName = ParamUtil.getString(actionRequest, "name"); String keywords = ParamUtil.getString(actionRequest, "keywords"); String description = ParamUtil.getString(actionRequest, "description"); if(Validator.isNotNull(user) && Validator.isNotNull(lessonIdAndUuid) && Validator.isNotNull(lessonName)){ long lessonId = Long.valueOf(lessonIdAndUuid.substring(6)); Lesson lesson = LessonLocalServiceUtil.findByIdAndOwnerId(lessonId, user.getUserId()); if(Validator.isNotNull(lesson)){ lesson.setName(lessonName); lesson.setKeywords(keywords); lesson.setDescription(description); lesson.setUpdateDate(new Date()); LessonLocalServiceUtil.updateLesson(lesson); } } } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); SessionMessages.add(actionRequest, "error"); } } @ProcessAction(name = "setPasswordPage") public void setPasswordPage(ActionRequest actionRequest, ActionResponse actionRresponse) { try { logger.info(" page = setPasswordPage" ); User user = PortalUtil.getUser(actionRequest); String lessonIdAndUuid = ParamUtil.getString(actionRequest, "id"); logger.info(" lessonIdAndUuid = " +lessonIdAndUuid); if(Validator.isNotNull(user) && Validator.isNotNull(lessonIdAndUuid)){ long lessonId = Long.valueOf(lessonIdAndUuid.substring(6)); Lesson lesson = LessonLocalServiceUtil.findByIdAndOwnerId(lessonId, user.getUserId()); if(Validator.isNotNull(lesson)){ actionRequest.setAttribute("page", "/setPassword.jsp"); actionRequest.setAttribute("lesson" , lesson); actionRequest.setAttribute("isProtected" , lesson.isIsProtected()); } } } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); SessionMessages.add(actionRequest, "error"); } } @ProcessAction(name = "setPassword") public void setPassword(ActionRequest actionRequest, ActionResponse actionRresponse) { logger.info("editLessonName"); try { User user = PortalUtil.getUser(actionRequest); String lessonIdAndUuid = ParamUtil.getString(actionRequest, "id"); String password = ParamUtil.getString(actionRequest, "password"); Boolean isProtected = ParamUtil.getBoolean(actionRequest, "isProtected"); password = EncoderUtil.encodeByMD5(password); if(Validator.isNotNull(user) && Validator.isNotNull(lessonIdAndUuid) && Validator.isNotNull(password)){ long lessonId = Long.valueOf(lessonIdAndUuid.substring(6)); Lesson lesson = LessonLocalServiceUtil.findByIdAndOwnerId(lessonId, user.getUserId()); if(Validator.isNotNull(lesson)){ lesson.setPassword(password); lesson.setIsProtected(isProtected); lesson.setUpdateDate(new Date()); LessonLocalServiceUtil.updateLesson(lesson); } } } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); SessionMessages.add(actionRequest, "error"); } } @ProcessAction(name = "deleteLesson") public void deleteLesson(ActionRequest actionRequest, ActionResponse actionRresponse) { logger.info("delete"); try { User user = PortalUtil.getUser(actionRequest); String lessonIdAndUuid = ParamUtil.getString(actionRequest, "id"); String password = ParamUtil.getString(actionRequest, "password"); password = EncoderUtil.encodeByMD5(password); if(Validator.isNotNull(user) && Validator.isNotNull(lessonIdAndUuid) && Validator.isNotNull(password)){ long lessonId = Long.valueOf(lessonIdAndUuid.substring(6)); Lesson lesson = LessonLocalServiceUtil.findByIdAndOwnerId(lessonId, user.getUserId()); if(Validator.isNotNull(lesson)){ LessonLocalServiceUtil.deleteLesson(lesson); } } } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); SessionMessages.add(actionRequest, "error"); } } @ProcessAction(name = "setShareModePage") public void setShareModePage(ActionRequest actionRequest, ActionResponse actionRresponse) { try { logger.info(" page = setShareModePage" ); User user = PortalUtil.getUser(actionRequest); String lessonIdAndUuid = ParamUtil.getString(actionRequest, "id"); logger.info(" lessonIdAndUuid = " +lessonIdAndUuid); if(Validator.isNotNull(user) && Validator.isNotNull(lessonIdAndUuid)){ long lessonId = Long.valueOf(lessonIdAndUuid.substring(6)); Lesson lesson = LessonLocalServiceUtil.findByIdAndOwnerId(lessonId, user.getUserId()); if(Validator.isNotNull(lesson)){ actionRequest.setAttribute("page", "/setShareMode.jsp"); actionRequest.setAttribute("lesson" , lesson); actionRequest.setAttribute("shareMode" , LessonShareMode.values()[lesson.getShareMode()].name()); List<UserGroup> userGroupList = new ArrayList<UserGroup>(); // logger.info("for test"); // logger.info(UserGroupLocalServiceUtil.getUserGroup(user.getCompanyId()).getName()); // List<UserGroup> userGroupList = UserGroupLocalServiceUtil.getUserUserGroups(user.getUserId()); // logger.info("for test2"); // for(UserGroup group : userGroupList){ //// logger.info("for test3"); // logger.info("group name = " + group.getName()); // } List<UserGroup> userGroupList2 = UserGroupLocalServiceUtil.getUserGroups(user.getCompanyId()); // logger.info("for test2"); for(UserGroup group : userGroupList2){ // logger.info("for test3"); if(group.getName().startsWith("lesson_")){ userGroupList.add(group); logger.info("group name = " + group.getName()); //TODO:for test logger.info("Previlidges " +group.getExpandoBridge().getAttribute("Previlidges")); } } logger.info("group list = " + userGroupList.toString()); Gson gson = new Gson(); actionRequest.setAttribute("userGroupList", gson.toJson(userGroupList)); } } } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); SessionMessages.add(actionRequest, "error"); } } @ProcessAction(name = "setShareMode") public void setShareMode(ActionRequest actionRequest, ActionResponse actionRresponse) { logger.info("setShareMode"); try { User user = PortalUtil.getUser(actionRequest); String lessonIdAndUuid = ParamUtil.getString(actionRequest, "id"); Integer shareMode = ParamUtil.getInteger(actionRequest, "shareMode"); Long userGroupId = ParamUtil.getLong(actionRequest, "userGroupId"); if(Validator.isNotNull(user) && Validator.isNotNull(lessonIdAndUuid) && Validator.isNotNull(shareMode)){ long lessonId = Long.valueOf(lessonIdAndUuid.substring(6)); Lesson lesson = LessonLocalServiceUtil.findByIdAndOwnerId(lessonId, user.getUserId()); if(Validator.isNotNull(lesson)){ lesson.setShareMode(shareMode); lesson.setUpdateDate(new Date()); if(LessonShareMode.GOURP.ordinal()==shareMode){ lesson.setUserGroupId(userGroupId); } LessonLocalServiceUtil.updateLesson(lesson); } } } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); SessionMessages.add(actionRequest, "error"); } } @ProcessAction(name = "replaceLessonPage") public void replaceLessonPage(ActionRequest actionRequest, ActionResponse actionRresponse) { try { logger.info(" page = setShareModePage" ); User user = PortalUtil.getUser(actionRequest); String lessonIdAndUuid = ParamUtil.getString(actionRequest, "id"); logger.info(" lessonIdAndUuid = " +lessonIdAndUuid); if(Validator.isNotNull(user) && Validator.isNotNull(lessonIdAndUuid)){ long lessonId = Long.valueOf(lessonIdAndUuid.substring(6)); Lesson lesson = LessonLocalServiceUtil.findByIdAndOwnerId(lessonId, user.getUserId()); if(Validator.isNotNull(lesson)){ actionRequest.setAttribute("page", "/replaceLesson.jsp"); actionRequest.setAttribute("lesson" , lesson); } } } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); SessionMessages.add(actionRequest, "error"); } } @ProcessAction(name = "replaceLesson") public void replaceLesson(ActionRequest actionRequest, ActionResponse actionRresponse) { InputStream ips =null; OutputStream ops =null; logger.info("replaceLesson"); try { String courseFolder = Utils.getValue("UPLOAD_PATH"); logger.info("courseFolder=" + courseFolder); User user = PortalUtil.getUser(actionRequest); UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest); // File docFile = uploadRequest.getFile("fileName"); ips = uploadRequest.getFileAsStream("fileName"); String lessonName = uploadRequest.getFileName("fileName"); String lessonIdAndUuid = uploadRequest.getParameter("id"); logger.info("lessonIdAndUuid = " + lessonIdAndUuid); if(Validator.isNotNull(user) && Validator.isNotNull(lessonIdAndUuid)){ long lessonId = Long.valueOf(lessonIdAndUuid.substring(6)); Lesson lesson = LessonLocalServiceUtil.findByIdAndOwnerId(lessonId, user.getUserId()); if(Validator.isNotNull(lesson)){ File file = new File(courseFolder+File.separator + lesson.getOwnerId()+File.separator +lesson.getFolderName()); logger.info("lesson path = " + file.getAbsoluteFile()); if(!file.exists()){ file.getParentFile().mkdirs(); }else{ FileUtils.deleteDirectory(file); } String lessonZip = courseFolder+File.separator + lesson.getOwnerId()+File.separator + lessonName; ops = new FileOutputStream(new File(lessonZip)); IOUtils.copy(ips, ops); unZipFile(lessonZip); logger.info("lessonZip = " + lessonZip); String lessonFolderName = lessonName.substring(0, lessonName.lastIndexOf(".")); String xmlFilePath = courseFolder+File.separator + lesson.getOwnerId()+File.separator +lessonFolderName + File.separator +"imsmanifest.xml"; String lessonTitle = XMLParser .parseXML(xmlFilePath); lesson.setName(lessonTitle); lesson.setFolderName(lessonFolderName); lesson.setUpdateDate(new Date()); lesson.setFileSize(uploadRequest.getSize("fileName")); LessonLocalServiceUtil.updateLesson(lesson); } } } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); SessionMessages.add(actionRequest, "error"); }finally{ if(ips!=null){ try { ips.close(); } catch (IOException e) { e.printStackTrace(); } } if(ops!=null){ try { ops.close(); } catch (IOException e) { e.printStackTrace(); } } } } @ProcessAction(name = "uploadLessonPage") public void uploadLessonPage(ActionRequest actionRequest, ActionResponse actionRresponse) { try { logger.info(" page = uploadLessonPage" ); User user = PortalUtil.getUser(actionRequest); if(Validator.isNotNull(user)){ actionRequest.setAttribute("page", "/uploadLesson.jsp"); } } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); SessionMessages.add(actionRequest, "error"); } } @ProcessAction(name = "uploadLesson") public void uploadLesson(ActionRequest actionRequest, ActionResponse actionRresponse) { InputStream ips =null; OutputStream ops =null; logger.info("uploadLesson"); try { String courseFolder = Utils.getValue("UPLOAD_PATH"); logger.info("courseFolder=" + courseFolder); User user = PortalUtil.getUser(actionRequest); UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest); ips = uploadRequest.getFileAsStream("fileName"); String lessonName = uploadRequest.getFileName("fileName"); if(Validator.isNotNull(user)){ String lessonFolderName = lessonName.substring(0, lessonName.lastIndexOf(".")); File file = new File(courseFolder+File.separator + user.getUserId()+File.separator +lessonFolderName); logger.info("lesson path = " + file.getAbsoluteFile()); if(!file.exists()){ file.getParentFile().mkdirs(); }else{ FileUtils.deleteDirectory(file); } String lessonZip = courseFolder+File.separator + user.getUserId()+File.separator + lessonName; ops = new FileOutputStream(new File(lessonZip)); IOUtils.copy(ips, ops); unZipFile(lessonZip); logger.info("lessonZip = " + lessonZip); String xmlFilePath = courseFolder+File.separator + user.getUserId()+File.separator +lessonFolderName + File.separator +"imsmanifest.xml"; String lessonTitle = XMLParser .parseXML(xmlFilePath); Lesson lesson = new LessonImpl(); Date date = new Date(); lesson.setUuid(Utils.getUuid()); lesson.setOwnerId(user.getUserId()); lesson.setName(lessonTitle); lesson.setFolderName(lessonFolderName); lesson.setFileSize(uploadRequest.getSize("fileName")); lesson.setRandomString(RandomStringUtils.randomNumeric(6)); lesson.setCreateDate(date); lesson.setUpdateDate(date); lesson.setShareMode(LessonShareMode.PRIVATE.ordinal()); LessonLocalServiceUtil.addLesson(lesson); } } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); SessionMessages.add(actionRequest, "error"); }finally{ if(ips!=null){ try { ips.close(); } catch (IOException e) { e.printStackTrace(); } } if(ops!=null){ try { ops.close(); } catch (IOException e) { e.printStackTrace(); } } } } @ProcessAction(name = "searchLesson") public void searchLesson(ActionRequest actionRequest, ActionResponse actionRresponse) { try { logger.info(" page = searchLesson" ); String search = ParamUtil.getString(actionRequest, "search"); if(Validator.isNotNull(search)){ actionRequest.setAttribute("search", search); } logger.info(" search =" + search); } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); SessionMessages.add(actionRequest, "error"); } } private void unZipFile(String strZipFile) throws ZipException, IOException { // UnzipFile unzipFile = new UnzipFile(); // String status = ""; long fileSize=0; // try { /* * STEP 1 : Create directory with the name of the zip file * * For e.g. if we are going to extract c:/demo.zip create c:/demo * directory where we can extract all the zip entries */ File fSourceZip = new File(strZipFile); String zipPath = strZipFile.substring(0, strZipFile.length() - 4); // String zipPath = fSourceZip.getParentFile().getAbsolutePath(); File temp = new File(zipPath); temp.mkdir(); temp.setExecutable(true); temp.setReadable(true); temp.setWritable(true); // status = zipPath; /* * STEP 2 : Extract entries while creating required sub-directories */ ZipFile zipFile = new ZipFile(fSourceZip); Enumeration e = zipFile.entries(); boolean createFolder = true; while (e.hasMoreElements()) { ZipEntry entry = (ZipEntry) e.nextElement(); File destinationFilePath = new File(zipPath, entry.getName()); destinationFilePath.setExecutable(true); destinationFilePath.setReadable(true); destinationFilePath.setWritable(true); // create directories if required. if (createFolder) { destinationFilePath.getParentFile().mkdirs(); } // if the entry is directory, leave it. Otherwise extract it. if (entry.isDirectory()) { continue; } else { createFolder = true; /* * Get the InputStream for current entry of the zip file * using * * InputStream getInputStream(Entry entry) method. */ BufferedInputStream bis = new BufferedInputStream( zipFile.getInputStream(entry)); int b; byte buffer[] = new byte[1024]; /* * read the current entry from the zip file, extract it and * write the extracted file. */ FileOutputStream fos = new FileOutputStream( destinationFilePath); BufferedOutputStream bos = new BufferedOutputStream(fos, 1024); while ((b = bis.read(buffer, 0, 1024)) != -1) { bos.write(buffer, 0, b); } // flush the output stream and close it. bos.flush(); bos.close(); // close the input stream. bis.close(); } fileSize = fileSize + destinationFilePath.length(); } zipFile.close(); fSourceZip.delete(); // } catch (IOException ioe) { // // ioe.printStackTrace(); // //logger.error("IOError :" + ioe.getMessage()); // } } private void searchContainerData(RenderRequest renderRequest,RenderResponse renderResponse) { long userId = PortalUtil.getUserId(renderRequest); logger.info(" userId =" + userId); String search=""; if(renderRequest.getAttribute("search")!=null){ search =(String) renderRequest.getAttribute("search"); }else{ search =ParamUtil.getString(renderRequest, "search"); } String searchQuery = "%"+ search+"%"; logger.info(" search =" + search); PortletConfig portletConfig = (PortletConfig)renderRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG); ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY); Map<String,String> paramMap= new HashMap<String,String>(); paramMap.put("search", search); List<Lesson> lessonList =new ArrayList<Lesson>(); /* * Creating IteratorURL and in that we will pass tab parameter */ PortletURL iteratorURL= renderResponse.createRenderURL(); Iterator<Map.Entry<String, String>> entries = paramMap.entrySet().iterator(); while (entries.hasNext()) { Map.Entry<String, String> entry = entries.next(); iteratorURL.setParameter(entry.getKey(), entry.getValue()); } /* * Creating SearchContainer */ SearchContainer<Lesson> searchContainer = new SearchContainer<Lesson>(renderRequest, null, null, SearchContainer.DEFAULT_CUR_PARAM, ParamUtil.getInteger(renderRequest, SearchContainer.DEFAULT_DELTA_PARAM, 10), iteratorURL, null, LanguageUtil.get(portletConfig, themeDisplay.getLocale(), "No Lessons were Found")); int total = 0; try { /* * Fetching all the User from the Current Company and added that into the userList */ // List<Lesson> lessons = LessonLocalServiceUtil.findByOwnerId(202); // List<Lesson> lessons = LessonLocalServiceUtil.findByNameOrKeywordsOrDescription(search, search, search); lessonList = LessonLocalServiceUtil.findByNameOrKeywordsOrDescriptionAndUserId(searchQuery, searchQuery, searchQuery, userId,searchContainer.getStart(), searchContainer.getEnd()); // String [] strs = new String[]{search}; // long [] longs = new long[]{userId}; // List<Lesson> lessons = LessonLocalServiceUtil.findByNameOrKeywordsOrDescriptionAndUserId(strs, strs, strs, longs); total = LessonLocalServiceUtil.countByNameOrKeywordsOrDescriptionAndUserId(searchQuery, searchQuery, searchQuery, userId); // lessonList = ListUtil.subList(lessons, searchContainer.getStart(), searchContainer.getEnd()); // if(lessons!=null){ // total = lessons.size(); // } } catch (SystemException e) { SessionErrors.add(renderRequest, SystemException.class.getName()); } /* * Sub listing the userList depending on the delta parameter we have set in the SearchContainer. */ searchContainer.setTotal(total); searchContainer.setResults(lessonList); renderRequest.setAttribute("searchContainer", searchContainer); } }
3. 问题:
liferay service builder 问题
需要的sql 语句: select * from xxx where (a like "%123%" or b like "%123%" or c like "%123%") and d='123'
liferay拼成的sql 语句select * from xxx where a like "%123%" and b like "%123%" and c like "%123%" and d='123'
每次service.xml 都会改变,需要重新再加上去