use of fi.otavanopisto.pyramus.dao.base.MagicKeyDAO in project pyramus by otavanopisto.
the class PyramusServletContextListener method contextInitialized.
/**
* Called when the application starts. Sets up both Hibernate and the page and JSON mappers needed
* to serve the client requests.
*
* @param servletContextEvent The servlet context event
*/
public void contextInitialized(ServletContextEvent servletContextEvent) {
try {
userTransaction.begin();
MagicKeyDAO magicKeyDAO = DAOFactory.getInstance().getMagicKeyDAO();
String applicationMagicKey = UUID.randomUUID().toString();
MagicKey magicKey = magicKeyDAO.findByApplicationScope();
if (magicKey != null) {
magicKeyDAO.updateName(magicKey, applicationMagicKey);
} else {
magicKeyDAO.create(applicationMagicKey, MagicKeyScope.APPLICATION);
}
Properties pageControllers = new Properties();
Properties jsonControllers = new Properties();
Properties binaryControllers = new Properties();
ServletContext ctx = servletContextEvent.getServletContext();
String webappPath = ctx.getRealPath("/");
// Load the system settings into the system properties
loadSystemSettings(System.getProperties());
// Load default page mappings from properties file
loadPropertiesFile(pageControllers, "pagemapping.properties");
// Load default JSON mappings from properties file
loadPropertiesFile(jsonControllers, "jsonmapping.properties");
// Load default binary mappings from properties file
loadPropertiesFile(binaryControllers, "binarymapping.properties");
// Initialize the page mapper in order to serve page requests
RequestControllerMapper.mapControllers(pageControllers, ".page");
// Initialize the JSON mapper in order to serve JSON requests
RequestControllerMapper.mapControllers(jsonControllers, ".json");
// Initialize the binary mapper in order to serve binary requests
RequestControllerMapper.mapControllers(binaryControllers, ".binary");
// Load plugins here so that plugins can override existing controllers
loadPlugins();
// Sets the application directory of the application, used primarily for initial data creation
System.getProperties().setProperty("appdirectory", webappPath);
// Register internal authentication provider
AuthenticationProviderVault.registerAuthenticationProviderClass("internal", InternalAuthenticationStrategy.class);
// Initializes all configured authentication strategies
AuthenticationProviderVault.getInstance().initializeStrategies();
if ("development".equals(System.getProperties().getProperty("system.environment"))) {
trustSelfSignedCerts();
}
if ("it".equals(System.getProperties().getProperty("system.environment"))) {
reindexHibernateEntities();
}
userTransaction.commit();
} catch (Exception e) {
try {
userTransaction.rollback();
} catch (Exception e1) {
e1.printStackTrace();
}
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
use of fi.otavanopisto.pyramus.dao.base.MagicKeyDAO in project pyramus by otavanopisto.
the class PyramusServletContextListener method contextDestroyed.
/**
* Called when the application shuts down.
*
* @param ctx The servlet context event
*/
public void contextDestroyed(ServletContextEvent ctx) {
try {
userTransaction.begin();
MagicKeyDAO magicKeyDAO = DAOFactory.getInstance().getMagicKeyDAO();
MagicKey magicKey = magicKeyDAO.findByApplicationScope();
if (magicKey != null) {
magicKeyDAO.delete(magicKey);
}
userTransaction.commit();
} catch (Exception e) {
try {
userTransaction.rollback();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
use of fi.otavanopisto.pyramus.dao.base.MagicKeyDAO in project pyramus by otavanopisto.
the class UploadStudentReportJSONRequestController method process.
/**
* Processes the request to edit a student group.
*
* @param requestContext
* The JSON request context
*/
public void process(JSONRequestContext requestContext) {
StudentFileDAO studentFileDAO = DAOFactory.getInstance().getStudentFileDAO();
FileTypeDAO fileTypeDAO = DAOFactory.getInstance().getFileTypeDAO();
StudentDAO studentDAO = DAOFactory.getInstance().getStudentDAO();
StaffMemberDAO userDAO = DAOFactory.getInstance().getStaffMemberDAO();
ReportDAO reportDAO = DAOFactory.getInstance().getReportDAO();
Long studentId = requestContext.getLong("studentId");
Long reportId = requestContext.getLong("reportId");
String reportParameters = requestContext.getString("reportParameters");
Long userId = requestContext.getLoggedUserId();
User loggedUser = userDAO.findById(userId);
String name = requestContext.getString("fileName");
Long fileTypeId = requestContext.getLong("fileType");
String contentType;
String format = requestContext.getString("reportFormat");
if ("doc".equals(format)) {
contentType = "application/msword";
} else {
format = "pdf";
contentType = "application/pdf";
}
Student student = studentDAO.findById(studentId);
FileType fileType = fileTypeId != null ? fileTypeDAO.findById(fileTypeId) : null;
Report report = reportDAO.findById(reportId);
MagicKeyDAO magicKeyDAO = DAOFactory.getInstance().getMagicKeyDAO();
String reportsContextPath = System.getProperty("reports.contextPath");
String reportsHost = System.getProperty("reports.host");
String reportsProtocol = System.getProperty("reports.protocol");
String reportsPort = System.getProperty("reports.port");
// output or preview
String outputMethod = "preview";
MagicKey magicKey = magicKeyDAO.findByApplicationScope();
StringBuilder urlBuilder = new StringBuilder().append(reportsProtocol).append("://").append(reportsHost).append(":").append(reportsPort).append(reportsContextPath).append("/").append(outputMethod).append("?magicKey=").append(magicKey.getName()).append("&__report=reports/").append(reportId).append(".rptdesign");
urlBuilder.append("&__format=").append(format);
urlBuilder.append(reportParameters);
try {
URL url = new URL(urlBuilder.toString());
URLConnection urlConn = url.openConnection();
InputStream inputStream = urlConn.getInputStream();
String fileId = null;
byte[] data = IOUtils.toByteArray(inputStream);
if (PyramusFileUtils.isFileSystemStorageEnabled()) {
try {
fileId = PyramusFileUtils.generateFileId();
PyramusFileUtils.storeFile(student, fileId, data);
data = null;
} catch (IOException e) {
fileId = null;
logger.log(Level.SEVERE, "Store user file to file system failed", e);
}
}
String reportName = report.getName().toLowerCase().replaceAll("[^a-z0-9\\.]", "_");
studentFileDAO.create(student, name, reportName + "." + format, fileId, fileType, contentType, data, loggedUser);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of fi.otavanopisto.pyramus.dao.base.MagicKeyDAO in project pyramus by otavanopisto.
the class ViewReportContentsViewController method process.
/**
* Processes the page request by including the corresponding JSP page to the response.
*
* @param pageRequestContext Page request context
*/
public void process(PageRequestContext pageRequestContext) {
MagicKeyDAO magicKeyDAO = DAOFactory.getInstance().getMagicKeyDAO();
Long reportId = pageRequestContext.getLong("reportId");
// output or preview
String outputMethod = "preview";
StringBuilder magicKeyBuilder = new StringBuilder().append(Long.toHexString(reportId)).append('-').append(Long.toHexString(System.currentTimeMillis())).append('-').append(Long.toHexString(Thread.currentThread().getId()));
MagicKey magicKey = magicKeyDAO.create(magicKeyBuilder.toString(), MagicKeyScope.REQUEST);
String localeAdd = "";
Locale locale = pageRequestContext.getRequest().getLocale();
if (locale != null) {
localeAdd = "&__locale=";
localeAdd += locale.getLanguage();
if (!StringUtils.isEmpty(locale.getCountry())) {
localeAdd += "_";
localeAdd += locale.getCountry();
}
}
StringBuilder urlBuilder = new StringBuilder().append(ReportUtils.getReportsUrl(pageRequestContext.getRequest())).append("/").append(outputMethod).append("?magicKey=").append(magicKey.getName()).append(localeAdd).append("&__report=reports/").append(reportId).append(".rptdesign");
// TODO: Report locale is wrong.
Map<String, String[]> parameterMap = pageRequestContext.getRequest().getParameterMap();
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
if (!reservedParameters.contains(entry.getKey())) {
String[] values = entry.getValue();
for (String value : values) {
// TODO ISO-8859-1 should be UTF-8, once Birt's parameter dialog form has its accept-charset="UTF-8" set
try {
urlBuilder.append('&').append(entry.getKey()).append('=').append(URLEncoder.encode(value, "ISO-8859-1"));
} catch (UnsupportedEncodingException e) {
throw new SmvcRuntimeException(e);
}
}
}
}
pageRequestContext.setIncludeUrl(urlBuilder.toString());
}
use of fi.otavanopisto.pyramus.dao.base.MagicKeyDAO in project pyramus by otavanopisto.
the class ViewReportParametersViewController method process.
/**
* Processes the page request by including the corresponding JSP page to the response.
*
* @param pageRequestContext Page request context
*/
public void process(PageRequestContext pageRequestContext) {
MagicKeyDAO magicKeyDAO = DAOFactory.getInstance().getMagicKeyDAO();
ReportDAO reportDAO = DAOFactory.getInstance().getReportDAO();
Long reportId = pageRequestContext.getLong("reportId");
Report report = reportDAO.findById(reportId);
StringBuilder magicKeyBuilder = new StringBuilder().append(Long.toHexString(reportId)).append('-').append(Long.toHexString(System.currentTimeMillis())).append('-').append(Long.toHexString(Thread.currentThread().getId()));
MagicKey magicKey = magicKeyDAO.create(magicKeyBuilder.toString(), MagicKeyScope.REQUEST);
String localeAdd = "";
Locale locale = pageRequestContext.getRequest().getLocale();
if (locale != null) {
localeAdd = "&__locale=";
localeAdd += locale.getLanguage();
if (!StringUtils.isEmpty(locale.getCountry())) {
localeAdd += "_";
localeAdd += locale.getCountry();
}
}
StringBuilder urlBuilder = new StringBuilder().append(ReportUtils.getReportsUrl(pageRequestContext.getRequest())).append("/parameter?magicKey=").append(magicKey.getName()).append(localeAdd).append("&__report=reports/").append(reportId).append(".rptdesign").append("&__masterpage=true&__nocache");
handleContextParameters(pageRequestContext, report, urlBuilder);
pageRequestContext.setIncludeUrl(urlBuilder.toString());
}
Aggregations