001/** 002 * Copyright 2011 The Kuali Foundation Licensed under the 003 * Educational Community License, Version 2.0 (the "License"); you may 004 * not use this file except in compliance with the License. You may 005 * obtain a copy of the License at 006 * 007 * http://www.osedu.org/licenses/ECL-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, 010 * software distributed under the License is distributed on an "AS IS" 011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 012 * or implied. See the License for the specific language governing 013 * permissions and limitations under the License. 014 */ 015 016package org.kuali.mobility.database.controllers; 017 018import org.apache.commons.io.IOUtils; 019import org.kuali.mobility.database.entity.DatabaseSchemaOutputForm; 020import org.kuali.mobility.database.service.DatabaseService; 021import org.kuali.mobility.database.validators.DatabaseSchemaOutputFormValidator; 022import org.springframework.beans.factory.annotation.Autowired; 023import org.springframework.stereotype.Controller; 024import org.springframework.ui.Model; 025import org.springframework.validation.BindingResult; 026import org.springframework.web.bind.annotation.ModelAttribute; 027import org.springframework.web.bind.annotation.RequestMapping; 028import org.springframework.web.bind.annotation.RequestMethod; 029 030import javax.servlet.http.HttpServletResponse; 031import java.io.ByteArrayInputStream; 032import java.io.IOException; 033import java.io.InputStream; 034import java.util.LinkedHashMap; 035import java.util.Map; 036 037@Controller 038@RequestMapping("/database") 039public class DatabaseController { 040 041 @Autowired 042 private DatabaseService service; 043 public void setDatabaseService(DatabaseService service) { 044 this.service = service; 045 } 046 047 private static final Map<String, String> dialectTypes; 048 049 static { 050 dialectTypes = new LinkedHashMap<String, String>(); 051 dialectTypes.put("", "Select a Database Type"); 052 dialectTypes.put("org.hibernate.dialect.Oracle10gDialect", "Oracle10g"); 053// dialectTypes.put("org.kuali.mobility.database.entity.KMEOracleDialect", "Oracle"); 054 dialectTypes.put("org.hibernate.dialect.MySQL5Dialect", "MySQL5"); 055// dialectTypes.put("org.kuali.mobility.database.entity.KMEMySql5Dialect", "MySQL5"); 056 dialectTypes.put("org.hibernate.dialect.SQLServerDialect", "SQL Server"); 057 dialectTypes.put("org.hibernate.dialect.PostgreSQLDialect", "PostgreSQL"); 058 dialectTypes.put("org.hibernate.dialect.DerbyDialect", "Derby"); 059 dialectTypes.put("org.hibernate.dialect.H2Dialect","H2"); 060// dialectTypes.put("", ""); 061 } 062 063 @RequestMapping(method = RequestMethod.GET) 064 public String index(Model uiModel) { 065// String schema = service.getSchema(); 066// uiModel.addAttribute("schema", schema); 067 DatabaseSchemaOutputForm form = new DatabaseSchemaOutputForm(); 068 uiModel.addAttribute("form", form); 069 uiModel.addAttribute("dialectTypes", dialectTypes); 070 return "database/schemaoutputform"; 071 } 072 073 @RequestMapping(method = RequestMethod.POST) 074 public String getSchema(Model uiModel, @ModelAttribute("form") DatabaseSchemaOutputForm form, BindingResult result) { 075 DatabaseSchemaOutputFormValidator validator = new DatabaseSchemaOutputFormValidator(); 076 validator.validate(form, result); 077 if (result.hasErrors()) { 078 uiModel.addAttribute("dialectTypes", dialectTypes); 079 return "database/schemaoutputform"; 080 } else { 081 String schema = this.getSchema(form.getDialectType(), form.getDelimiter(), form.isNewLineBeforeDelimiter(), form.isRemoveForeignKeys()); 082 uiModel.addAttribute("schema", schema); 083 uiModel.addAttribute("dialect", form.getDialectType()); 084 uiModel.addAttribute("delimiter", form.getDelimiter()); 085 return "database/schemaoutput"; 086 } 087 } 088 089 @RequestMapping(method = RequestMethod.POST, params = "download") 090 public String getSchemaDownload(Model uiModel, @ModelAttribute("form") DatabaseSchemaOutputForm form, BindingResult result, HttpServletResponse response) { 091 DatabaseSchemaOutputFormValidator validator = new DatabaseSchemaOutputFormValidator(); 092 validator.validate(form, result); 093 if (result.hasErrors()) { 094 uiModel.addAttribute("dialectTypes", dialectTypes); 095 return "database/schemaoutputform"; 096 } else { 097 try { 098 response.setContentType("text/plain"); 099 response.setHeader("Content-Disposition", "attachment;filename=schema.ddl"); 100 String schema = this.getSchema(form.getDialectType(), form.getDelimiter(), form.isNewLineBeforeDelimiter(), form.isRemoveForeignKeys()); 101 InputStream is = new ByteArrayInputStream(schema.getBytes("UTF-8")); 102 IOUtils.copy(is, response.getOutputStream()); 103 response.flushBuffer(); 104 } catch (IOException ex) { 105 // log.info("Error writing file to output stream. Filename was '" + 106 // fileName + "'"); 107 throw new RuntimeException("IOError writing file to output stream"); 108 } 109 return null; 110 } 111 } 112 113 /* 114 @RequestMapping(value="/schema", method = RequestMethod.GET) 115 @ResponseBody 116 public String getSchema(@RequestParam(value = "dialect", required = true) String dialect, @RequestParam(value = "delimiter", required = true) String delimiter, HttpServletRequest request) { 117// User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY); 118 String schema = service.getSchema(dialect, delimiter); 119 return schema; 120 } 121 */ 122 123 /* 124 @RequestMapping(value = "/schema2", method = RequestMethod.GET) 125 public void getFile(@RequestParam(value = "dialect", required = true) String dialect, @RequestParam(value = "delimiter", required = true) String delimiter, @RequestParam(value = "newline", required = true) boolean newLine, HttpServletResponse response) { 126 try { 127 response.setContentType("text/plain"); 128 response.setHeader("Content-Disposition", "attachment;filename=schema.ddl"); 129 String schema = this.getSchema(dialect, delimiter, newLine); 130 InputStream is = new ByteArrayInputStream(schema.getBytes("UTF-8")); 131 IOUtils.copy(is, response.getOutputStream()); 132 response.flushBuffer(); 133 } catch (IOException ex) { 134 // log.info("Error writing file to output stream. Filename was '" + 135 // fileName + "'"); 136 throw new RuntimeException("IOError writing file to output stream"); 137 } 138 } 139 */ 140 141 private String getSchema(String dialect, String delimiter, boolean newLine, boolean overrideAlterTable) { 142 if (newLine) { 143 delimiter = "\n" + delimiter; 144 } 145 String schema = service.getSchema(dialect, delimiter, overrideAlterTable); 146 return schema; 147 } 148 149 150}