/* 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. */ package no.dusken.aranea.admin.control; import no.dusken.aranea.admin.editor.CalendarEditor; import no.dusken.aranea.admin.editor.GenericCollectionEditor; import no.dusken.aranea.admin.editor.GenericEditor; import no.dusken.aranea.model.*; import no.dusken.aranea.service.PageService; import no.dusken.aranea.service.SectionPageService; import no.dusken.aranea.service.SectionService; import no.dusken.aranea.service.TagService; import no.dusken.common.service.GenericService; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Predicate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Required; import org.springframework.validation.BindException; import org.springframework.validation.Errors; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.List; import java.util.Map; public class EditPageController<T extends Page> extends GenericEditController<Page> { private GenericService<PageImage> pageImageService; protected PageService pageService; private GenericEditor<Section> sectionEditor; private GenericCollectionEditor<Tag, List> tagsEditor; private CalendarEditor timeEditor; private SectionService sectionService; private TagService tagService; private GenericCollectionEditor<Person, List> authorsEditor; private SectionPageService sectionPageService; /** * If set to true a SectionPage with ordering 1 is created in this Page's * parent section */ private Boolean createParentSectionPage = true; /** * If set to true a SectionPage with ordering 1 is created in the frontpage */ private Boolean createFrontpageSectionPage = true; /** * The name of the section that we should create additional * SectionPages for. * For instance Gallery */ private String customSectionSectionPage; public EditPageController(Class type) { super(type); } @Override protected Object formBackingObject(HttpServletRequest request) { Page page = (Page) super.formBackingObject(request); if(page.getVisibleTo() == null){ Calendar cal = new GregorianCalendar(); cal.add(GregorianCalendar.YEAR, 1000); page.setVisibleTo(cal); } if(page.getVisibleFrom() == null){ page.setVisibleFrom(new GregorianCalendar()); } return page; } @Override protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object object, BindException errors) throws Exception { Page page = (Page) object; for (PageImage pi : page.getPageImages()) { if (request.getParameter("deleteEntry" + pi.getID()) != null) { // we are to delete this entry page.removeEntry(pi); if(pi == page.getFrontpageImage()){ page.setFrontpageImage(null); //TODO remove this hack, it should be handled be the db and cascading pageService.saveOrUpdate(page); } // the entity is detached (?) entach it pi = pageImageService.getEntity(pi.getID()); // delete the entry from the database pageImageService.remove(pi); // go back to the edit view setSuccessView(getFormView()); // do not continue the loop break; }else if(request.getParameter("setAsMainImage" + pi.getID()) != null){ page.setFrontpageImage(pi); // do not continue the loop break; } } if(request.getParameter("unSetMainImage") != null){ page.setFrontpageImage(null); } // Detatch frontpage image if we are going to delete. if (request.getParameter("delete") != null) { page.setFrontpageImage(null); genericService.saveOrUpdate(page); } page.setModified(Calendar.getInstance()); boolean createSectionPage = false; if(page.isNew()){ createSectionPage = true; } ModelAndView mav = super.onSubmit(request, response, page, errors); page = (Page) mav.getModel().get("entity"); if(createSectionPage){ // At this point we expect that the object has been saved to the database and that the // reference is valid createSectionPage(page); } return mav; } private void createSectionPage(Page page) { if(createParentSectionPage){ Section s = sectionService.getEntity(page.getParent().getID()); doCreateSectionPage(page, s); } if(createFrontpageSectionPage){ Section s = sectionService.getSectionByUrl("frontpage", null); doCreateSectionPage(page, s); } if(customSectionSectionPage != null && customSectionSectionPage.length() > 0){ Section s = sectionService.getSectionByUrl(customSectionSectionPage, null); if(s == null){ s = new Section(); s.setName(customSectionSectionPage); s.setHidden(true); s.setActive(true); s.setUrl(customSectionSectionPage.toLowerCase()); s = sectionService.saveOrUpdate(s); } doCreateSectionPage(page, s); } } private void doCreateSectionPage(Page page, Section section){ SectionPage sp = new SectionPage(section, page, 1, ""); List<SectionPage> sps = sectionPageService.getSectionPageByOwner(section); sps.add(0, sp); sectionPageService.cascadeAndSaveOrUpdateSectionPages(sps); } protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { super.initBinder(request, binder); binder.registerCustomEditor(Section.class, sectionEditor); binder.registerCustomEditor(List.class, "tags", tagsEditor); binder.registerCustomEditor(Calendar.class, timeEditor); binder.registerCustomEditor(List.class, "authors", authorsEditor); // to actually be able to convert Multipart instance to byte[] // we have to register a custom editor binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); binder.setBindEmptyMultipartFiles(false); // now Spring knows how to handle multipart object and convert them /*dont allow properties like published to be binded, so that it is not possible to set published to true * by tampering with the formsubmission * */ String[] disallowedFields = new String[]{"published, modified, timePublished, " + "topic, sectionPriority, frontsidePriority"}; binder.setDisallowedFields(disallowedFields); } @Override protected Map referenceData(HttpServletRequest request, Object object, Errors errors) throws Exception { Map<String, Object> map = super.referenceData(request, object, errors); map.put("formatter", new SimpleDateFormat(timeEditor.getFormat())); map.put("format", timeEditor.getFormat()); map.put("sections", getSectionsExceptFrontPage()); map.put("tags", tagService.getTags()); map.put("persons", personService.getPersonsByFirstname(true)); Page page = (Page) object; int nr = 0; //get the highest paragraphnumber for(PageImage pi: page.getPageImages()){ if(pi.getParagraph() >= nr){ nr = pi.getParagraph(); } } //and add 1 map.put("nextParagraph", ++nr); return map; } private List<Section> getSectionsExceptFrontPage() { List<Section> sections = sectionService.getSections(true); CollectionUtils.filter(sections, new Predicate() { @Override public boolean evaluate(Object object) { return !((Section) object).getUrl().equals("frontpage"); } }); return sections; } @Required @Autowired public void setPageImageService(GenericService<PageImage> pageImageService) { this.pageImageService = pageImageService; } @Required @Autowired public void setPageService(PageService pageService) { this.pageService = pageService; } @Required public void setSectionEditor(GenericEditor<Section> sectionEditor) { this.sectionEditor = sectionEditor; } @Required public void setTagsEditor(GenericCollectionEditor<Tag, List> tagsEditor) { this.tagsEditor = tagsEditor; } @Required public void setTimeEditor(CalendarEditor timeEditor) { this.timeEditor = timeEditor; } @Required @Autowired public void setSectionService(SectionService sectionService) { this.sectionService = sectionService; } @Required public void setAuthorsEditor(GenericCollectionEditor<Person, List> authorsEditor) { this.authorsEditor = authorsEditor; } @Required @Autowired public void setTagService(TagService tagService) { this.tagService = tagService; } public void setCreateParentSectionPage(Boolean createParentSectionPage) { this.createParentSectionPage = createParentSectionPage; } public void setCreateFrontpageSectionPage(Boolean createFrontpageSectionPage) { this.createFrontpageSectionPage = createFrontpageSectionPage; } public void setCustomSectionSectionPage(String customSectionSectionPage) { this.customSectionSectionPage = customSectionSectionPage; } @Autowired public void setSectionPageService(SectionPageService sectionPageService) { this.sectionPageService = sectionPageService; } }