package no.dusken.aranea.admin.control;
import no.dusken.aranea.model.Page;
import no.dusken.aranea.model.PageRelation;
import no.dusken.aranea.service.PageRelationService;
import no.dusken.aranea.service.PageService;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
/*
Copyright 2006 - 2010 Under Dusken
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
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.
@author Marvin Bredal Lillehaug (lillehau@underdusken.no)
*/
public class EditPageRelationsController extends SimpleFormController {
private PageService pageService;
private PageRelationService pageRelationService;
@Override
protected Object formBackingObject(HttpServletRequest request) {
//getting the referencepage
Long pageID = ServletRequestUtils.getLongParameter(request,
"referencePage", 0);
Page referencePage = pageService.getEntity(pageID);
return referencePage.getPageRelations();
}
@Override
protected Map referenceData(HttpServletRequest request, Object object, Errors errors) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
//getting the referencepage
Long pageID = ServletRequestUtils.getLongParameter(request,
"referencePage", 0);
Page referencePage = pageService.getEntity(pageID);
map.put("referencePage", referencePage);
return map;
}
@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object object,
BindException errors) throws Exception {
Long pageID = ServletRequestUtils.getLongParameter(request,
"referencePage", 0);
Page referencePage = pageService.getEntity(pageID);
//delete or change a relation?
for (PageRelation pr : referencePage.getPageRelations()) {
if (request.getParameter("deleteEntry" + pr.getID()) != null) {
// we are to delete this entry
// the entity is detached (?) entach it
pr = pageRelationService.getEntity(pr.getID());
// delete the entry from the database
pageRelationService.remove(pr);
// go back to the edit view
setSuccessView(getFormView());
// do not continue the loop
break;
}else{
Page relatedPage = pr.getPageRelatedTo(referencePage);
String description1 = ServletRequestUtils.getStringParameter(request, "description1" + relatedPage.getID(), "");
String description2 = ServletRequestUtils.getStringParameter(request, "description2" + relatedPage.getID(), "");
boolean onFrontPage = ServletRequestUtils.getBooleanParameter(request, "onFrontPage", false);
pr.setDescription1(description1);
pr.setDescription2(description2);
pr.setOnFrontPage(onFrontPage);
if(referencePage.equals(pr.getPage1())){
pr.setParagraphPage1(ServletRequestUtils.getIntParameter(request, "paragraphfor" + relatedPage.getID(), 0));
}
pageRelationService.saveOrUpdate(pr);
}
}
ModelAndView mav = super.onSubmit(request, response, object, errors);
mav.getModel().put("referencePage", referencePage);
return mav;
}
@Required
public void setPageService(PageService pageService) {
this.pageService = pageService;
}
@Required
public void setPageRelationService(PageRelationService pageRelationService) {
this.pageRelationService = pageRelationService;
}
}