Java tutorial
/* * SurveyPanel * Copyright (C) 2009 Serge Tan Panza * All rights reserved. * License: GNU/GPL License v3 , see LICENSE.txt * SurveyPanel is free software. This version may have been modified pursuant * to the GNU General Public License, and as distributed it includes or * is derivative of works licensed under the GNU General Public License or * other free or open source software licenses. * See COPYRIGHT.txt for copyright notices and details. * */ package com.surveypanel.form; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; import org.springframework.context.MessageSource; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import com.surveypanel.domain.Script; import com.surveypanel.domain.SurveyPanel; import com.surveypanel.utils.StringUtils; import freemarker.cache.FileTemplateLoader; /** * @author stanpanza * */ public class InMemoryFormFactory extends AbstractFormFactory { private Map<String, FormDTO> forms = new HashMap<String, FormDTO>(); private Map<Long, List<Script>> scripts = new HashMap<Long, List<Script>>(); private String path; private Serializer serializer = new Persister(); public InMemoryFormFactory(String path) { this.path = path; } public void delete(String formGuid, long surveyId) { if (forms.containsKey(formGuid)) { forms.remove(formGuid); } } public FileTemplateLoader getTemplateLoader(long surveyId) { URL resource = getClass().getResource("/templates/"); try { return new FileTemplateLoader(new File(resource.getFile())); } catch (IOException e) { logger.error(e); } return null; } public boolean update(Form form) throws Exception { FormDTO formDTO = form.toDTO(); formDTO.setUpdated(new Date()); forms.put(form.getId(), formDTO); return true; } public FormDTO getFormDTO(String formId, long surveyId) { return forms.get(formId); } public SurveyPanel loadDefinition(long surveyId) { try { return serializer.read(SurveyPanel.class, getClass().getResourceAsStream(path + surveyId + "-survey.xml")); } catch (Exception e) { logger.error(e); } return null; } public Questionnaire loadQuestionnaire(QDefinition qDefinition) { String flowPath = "/js/" + qDefinition.getId() + "/flow.js"; return new Questionnaire(qDefinition, StringUtils.getFileContent(new String[] { flowPath }), qDefinition.getId()); } public void init(long surveyId) { } public boolean add(Form form) throws Exception { forms.put(form.getId(), form.toDTO()); return true; } @Override public MessageSource getMessageSource(long surveyId) { ReloadableResourceBundleMessageSource msg = new ReloadableResourceBundleMessageSource(); msg.setBasename("messages"); msg.setDefaultEncoding("UTF-8"); msg.setCacheSeconds(10); msg.setUseCodeAsDefaultMessage(true); return msg; } @Override public List<Script> getScripts(long surveyId) { List<Script> list = scripts.get(surveyId); if (list == null) { String fileName = "/js/" + surveyId + "flow.js"; String source = StringUtils.getFileContent(new String[] {}); Date now = new Date(); List<Script> foundList = new ArrayList<Script>(); Script script = new Script(); script.setCreatedDate(now); script.setLastUpdated(now); script.setName(fileName); script.setSource(source); foundList.add(script); } return list; } }