如何在liferay搜索容器文本中对异构数据进行排序?

时间:2017-11-04 23:12:47

标签: java sorting collections java-7 liferay-6

我解释了我的问题:

我正在使用Liferay 6.1,我正在尝试对<liferay-ui:search-container-column-text>标记中的日期进行排序。

在我的portlet视图中,我有一个表格,显示文档列表,其中一个字段是修改日期。默认情况下,此日期是文件修改日期。

在“文档和媒体”控制面板中,我创建了一个文档类型,并在名为date(可选)的文档类型中添加了一个字段,以手动添加日期。

在java代码中我需要检查这个可选的日期字段,如果它是空的我保留旧代码(显示文档的修改日期)如果它不是空的那么我想显示它的值(值)可选日期)。 我创建了一个文档类型,并在文档类型中添加了一个名为可选日期的字段来手动添加日期。

现在,日期列可以包含默认文件日期可选日期

目标: 我想按升序排序日期。

下图显示了详细信息:

enter image description here

在view.jsp中:

    <form action="#" id="download_form">
                   <%
                    String orderByCol, orderByType;
                    PortalPreferences portalPrefs = PortletPreferencesFactoryUtil.getPortalPreferences(request); 
                    String sortByCol = ParamUtil.getString(request, "orderByCol"); 
                    String sortByType = ParamUtil.getString(request, "orderByType"); 

                    if (Validator.isNotNull(sortByCol ) && Validator.isNotNull(sortByType )) { 

                    portalPrefs.setValue("NAME_SPACE", "sort-by-col", sortByCol); 
                    portalPrefs.setValue("NAME_SPACE", "sort-by-type", sortByCol); 

                    } else { 

                    orderByCol = portalPrefs.getValue("NAME_SPACE", "sort-by-col", "date");
                    orderByType = portalPrefs.getValue("NAME_SPACE", "sort-by-type ", "asc");   
                    }
                    PortletURL actionURL = renderResponse.createRenderURL(); 
                %>
        <%!
        DateFormat dateFormat = new SimpleDateFormat("MMM yyyy");
        %>

        <aui:button id="download_files" type="submit" value="Download"></aui:button>
        <br /> <br />

        <liferay-ui:search-container  orderByCol="<%=sortByCol%>"
            orderByType="<%=sortByType%>"
            iteratorURL="<%=actionURL%>" delta="10"
            emptyResultsMessage="no-documents">
            <liferay-ui:search-container-results>
                        <%
                        List<DLFileEntry> fileList = (List<DLFileEntry>) request.getAttribute("listFiles");
                        //OrderByComparator orderByComparator = OrderByComparatorFactoryUtil.create("DLFileEntry", "modifiedDate", false);

                        ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
                        OrderByComparator orderByComparator =        
                        CustomComparatorUtil.getFileOrderByComparator(sortByCol, sortByType, themeDisplay);         
                        Collections.sort(fileList, orderByComparator);

                        results = ListUtil.subList(fileList, searchContainer.getStart(),    
                                     searchContainer.getEnd());

                       if (fileList.size()< total)
                        {
                           total = fileList.size();
                        }
                       pageContext.setAttribute("results", results);
                       pageContext.setAttribute("total", total);
                    %>
            </liferay-ui:search-container-results>
            <liferay-ui:search-container-row modelVar="file" className="DLFileEntry">
                <%
                    String fileDate = dateFormat.format(file.getModifiedDate());
                    try{
                        Map<String, Fields> fieldsMap = file.getFieldsMap(file.getFileVersion().getFileVersionId());
                        Collection<Fields> collectionFields = fieldsMap.values();
                        List<Fields> listFields = new ArrayList<Fields>(collectionFields);
                        for (Fields fields : listFields) {
                            if(fields.get("optionaldate") != null ) {
                                  DateFormat inputFormat = new SimpleDateFormat("yyyy/MM/dd");
                                  Date optionalDate = inputFormat.parse((String) fields.get("optionaldate").getValue());
                                  fileDate = dateFormat.format(optionalDate);
                            }
                        }
                    }catch(Exception ex){
                        ex.printStackTrace();
                    }
                %>


                <liferay-ui:search-container-column-text name='date'
                    cssClass="txt-capitalize width-10"
                    value="<%=fileDate%>" />


            </liferay-ui:search-container-row>
            <liferay-ui:search-iterator
                searchContainer="<%= searchContainer %>"
                paginate="${fn:length(listFiles) ge 10}" />
        </liferay-ui:search-container>
    </form>

问题:

我需要更改代码以便以干净的方式对日期进行排序,我是否必须在JSP或portlet类中执行排序逻辑?

更新

我尝试创建自定义比较器。

注意: 我修改了view.jsp代码并添加了两个类。

CustomComparatorUtil.java:

public class CustomComparatorUtil {
    public static OrderByComparator getFileOrderByComparator(String orderByCol,
            String orderByType, ThemeDisplay themeDisplay) {

        boolean orderByAsc = false;

        if (orderByType.equals("asc")) {
            orderByAsc = true;
        }

        OrderByComparator orderByComparator = null;

        if (orderByCol.equalsIgnoreCase("date")) {
            System.out.println("themeDisplay: "+themeDisplay);
            orderByComparator = new ModifiedDateComparator(orderByAsc, themeDisplay);
        }

        return orderByComparator;
    }
}

ModifiedDateComparator.java:

public class ModifiedDateComparator extends OrderByComparator {

public static String ORDER_BY_ASC = "status ASC";
public static String ORDER_BY_DESC = "status DESC";

DateFormat dateFormat = new SimpleDateFormat("MMM yyyy");

public ThemeDisplay themeDisplay;

public ModifiedDateComparator() {
    this(false, null);
}

public ModifiedDateComparator(boolean asc, ThemeDisplay themeDisplay) {
    _asc = asc;
    this.themeDisplay = themeDisplay;
}

@Override
public int compare(Object obj1, Object obj2) {

    DLFileEntry instance1 = (DLFileEntry) obj1;
    DLFileEntry instance2 = (DLFileEntry) obj2;

    if (instance1 == null || instance2 == null) {
        return 0;
    }

    List<Date> listOptionalDate = getListOptionalDate(themeDisplay);
    int value = 0;
    for (Iterator<Date> i = listOptionalDate.iterator(); i.hasNext();) {
        Date optionalDate = i.next();
        System.out.println(optionalDate);
        if(optionalDate != null)
            value = instance1.getModifiedDate().compareTo(optionalDate);
        else
            value = instance1.getModifiedDate().compareTo(instance2.getModifiedDate());
    }

    if (_asc) {
        return value;
    } else {
        return -value;
    }

}

public String getOrderBy() {

    if (_asc) {
        return ORDER_BY_ASC;
    } else {
        return ORDER_BY_DESC;
    }
}

private boolean _asc;

private List<Date> getListOptionalDate(ThemeDisplay themeDisplay) {
    List<DLFileEntry> listFiles = new ArrayList<DLFileEntry>();
    FlashPortlet flashPortlet = new FlashPortlet();
    try {
        listFiles = flashPortlet.getAllFiles(themeDisplay);
    } catch (PortalException e1) {
        e1.printStackTrace();
    } catch (SystemException e1) {
        e1.printStackTrace();
    }
    List<Date> listDate = new ArrayList<Date>();
    for (DLFileEntry file : listFiles) {
        try {
            Map<String, Fields> fieldsMap = file.getFieldsMap(file
                    .getFileVersion().getFileVersionId());
            Collection<Fields> collectionFields = fieldsMap.values();
            for (Fields fields : collectionFields) {
                if (fields.get("optionaldate") != null) {
                    DateFormat inputFormat = new SimpleDateFormat(
                            "yyyy/MM/dd");
                    Date optionalDate = inputFormat.parse((String) fields
                            .get("optionaldate").getValue());
                    listDate.add(optionalDate);
                } else {
                    listDate.add(null);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return listDate;
  }
}

0 个答案:

没有答案