/*
* This file was automatically generated by MD4J.
*
* Licensed under the GNU General Public License, Version 2.0 (the "License") or above;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* *http://www.gnu.org/licenses/gpl.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package gr.abiss.md4j.sampledomain.web;
import java.io.Serializable;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.MultipartRequestHandler;
import org.apache.struts.action.Action;
import gr.abiss.md4j.dao.Page;
import gr.abiss.md4j.util.StringUtil;
import gr.abiss.md4j.sampledomain.web.CitySearchForm;
import gr.abiss.md4j.sampledomain.business.CityManagerLocal;
import gr.abiss.md4j.sampledomain.business.CityManagerUtil;
/**
* @struts.action path="/City/Search"
* name="CitySearchForm"
* input="/City/Search.jsp"
* scope="request"
* validate="true"
*
* @struts.action-forward path="/City/Search.jsp"
* name="CitySearchActionView"
*
*/
public class CitySearchAction extends Action{
private static Logger log = Logger.getLogger(CitySearchAction.class);
/**
* A static, unmodifiable Set holding the projection properties.
* If you want these created at runtime in each execute method call,
* simply set md4j:use-instance-projections to true.
*/
private static Set projectionProps = new HashSet();
static{
projectionProps.add("id");
projectionProps.add("name");
projectionProps.add("region");
projectionProps.add("population");
projectionProps.add("country.id");
projectionProps.add("country.id");
projectionProps = java.util.Collections.unmodifiableSet(projectionProps);
}
/**
* @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
String fwd = "error";
try{
CitySearchForm form = (CitySearchForm) actionForm;
Map params = form.toMap();
// check if results need to be produced
if(params.size() > 0 || form.getOrder().getOrderProperties().size() > 0){
CityManagerLocal manager = CityManagerUtil.getLocalHome().create();
Integer pageNumber = form.getResultsPageNumber();
// obtain a page of results and put it under in request context for the view
Page page = manager.search(CitySearchAction.projectionProps, params, form.getOrder(), pageNumber != null?pageNumber.intValue():1, 10);
request.setAttribute("results", page);
// populate form bean option data for drop downs
form.setParentSelectOptions(page.getParentOptions());
}
else{
CityManagerLocal manager = CityManagerUtil.getLocalHome().create();
form.setParentSelectOptions(manager.getParentOptions());
}
fwd = "CitySearchActionView";
} catch (Exception e) {
log.error("Failed to perform search operation, wrapping to RuntimeException", e);
throw new RuntimeException(e);
}
return mapping.findForward(fwd);
}
}