SearchAction.java
/**
* @ Copyright 2013 FA Software; All right reserved. No part of this program may be reproduced or transmitted
* in any form or by any means, electronic or mechanical, including photocopying, recording, or by any
* information storage or retrieval system without written permission from FA Software, except for inclusion
* of brief quotations in a review.
*/
package com.mycim.webapp.actions.search;
import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.mycim.framework.jdbc.Page;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.consts.SystemConstants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.AbstractAction;
import com.mycim.webapp.forms.SearchForm;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* @author Andy
*/
public class SearchAction extends AbstractAction {
@Override
public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
String type = request.getParameter("type");
String targetIds = request.getParameter("targetIds");
String upperCaseFlag = request.getParameter("upperCaseFlag");
String dataPermission = request.getParameter("dataPermission");
/* String keyValue = new String(request.getParameter("keyValue").getBytes("ISO8859-1"), "UTF-8"); */
String keyValue = StringUtils.trim(request.getParameter("keyValue"));
String callback = request.getParameter("callback");
String referceIds = request.getParameter("referceIds");
String searchcolume = request.getParameter("searchcolume");
Assert.isFalse(type == null,
Errors.create().key(MessageIdList.SEARCH_TYPE_NOT_CONFIGED).content("未配置type!").build());
Assert.isFalse(targetIds == null,
Errors.create().key(MessageIdList.SEARCH_ID_NOT_CONFIGED).content("未配置targetids!").build());
request.setAttribute("type", type);
request.setAttribute("id", targetIds);
request.setAttribute("keyValue", keyValue);
request.setAttribute("callback", callback);
request.setAttribute("referceIds", referceIds);
request.setAttribute("callbackClick", request.getParameter("callbackClick"));
request.setAttribute("callbackFlag", request.getParameter("callbackFlag"));
request.setAttribute("searchcolume", searchcolume);
request.setAttribute("upperCaseFlag", upperCaseFlag);
request.setAttribute("dataPermission", StringUtils.defaultIfBlank(dataPermission, Boolean.FALSE.toString()));
return mapping.findForward("init");
}
public Page search(SearchForm theform) {
Map data = theform.getParameters();
String upperCaseFlag = MapUtils.getString(data, "upperCaseFlag");
String dataPermission = theform.getDataPermission();
Map condtionMap;
if (StringUtils.equals("false", upperCaseFlag)) {
data.remove("upperCaseFlag");
condtionMap = WebUtils.parseCondtionWithNoUpperCase(data);
} else {
condtionMap = WebUtils.parseCondtion(data);
}
String buildSql = buildSearchSql(theform, condtionMap);
Page page = new Page(theform.getPage(), theform.getLimit());
return Boolean.TRUE.toString().equalsIgnoreCase(dataPermission)?
baseService.searchResults4Permission(page, condtionMap, buildSql):
baseService.searchResults(page, condtionMap, buildSql);
}
private String buildSearchSql(SearchForm theForm, Map condtionMap) {
StringBuffer searchSql = new StringBuffer();
searchSql.append("SELECT ").append(theForm.getColumns()).append(" FROM ").append("(SELECT * FROM ")
.append(theForm.getTableID());
if (StringUtils.isNotBlank(theForm.getStrWhere())) {
searchSql.append(" WHERE " + theForm.getStrWhere());
}
searchSql.append(") search_0 ");
if (!condtionMap.isEmpty()) {
searchSql.append(" WHERE ");
Set condtionKeys = condtionMap.keySet();
int count = 0;
for (Iterator iterator = condtionKeys.iterator(); iterator.hasNext(); ) {
String condtionKey = (String) iterator.next();
searchSql.append(condtionKey);
if (StringUtils.indexOf(MapUtils.getString(condtionMap, condtionKey), "*") > -1) {
searchSql.append(" LIKE ");
} else {
searchSql.append(" = ");
}
searchSql.append(" ? ");
if (count != condtionKeys.size() - 1) {
searchSql.append(" AND ");
}
count++;
}
}
if (StringUtils.isNotBlank(theForm.getOrderBy())) {
searchSql.append("ORDER BY ").append(theForm.getOrderBy());
} else {
searchSql.append("ORDER BY ").append(theForm.getKeyColumn());
if (StringUtils.isNotBlank(theForm.getDescFlag())) {
searchSql.append(theForm.getDescFlag());
}
}
return searchSql.toString();
}
}