View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.mobility.database.controllers;
17  
18  import org.apache.commons.io.IOUtils;
19  import org.kuali.mobility.database.entity.DatabaseSchemaOutputForm;
20  import org.kuali.mobility.database.service.DatabaseService;
21  import org.kuali.mobility.database.validators.DatabaseSchemaOutputFormValidator;
22  import org.springframework.beans.factory.annotation.Autowired;
23  import org.springframework.stereotype.Controller;
24  import org.springframework.ui.Model;
25  import org.springframework.validation.BindingResult;
26  import org.springframework.web.bind.annotation.ModelAttribute;
27  import org.springframework.web.bind.annotation.RequestMapping;
28  import org.springframework.web.bind.annotation.RequestMethod;
29  
30  import javax.servlet.http.HttpServletResponse;
31  import java.io.ByteArrayInputStream;
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.util.LinkedHashMap;
35  import java.util.Map;
36  
37  @Controller 
38  @RequestMapping("/database")
39  public class DatabaseController {
40  
41      @Autowired
42      private DatabaseService service;
43      public void setDatabaseService(DatabaseService service) {
44          this.service = service;
45      }
46  	
47      private static final Map<String, String> dialectTypes;
48      
49      static {
50      	dialectTypes = new LinkedHashMap<String, String>();
51      	dialectTypes.put("", "Select a Database Type");
52      	dialectTypes.put("org.hibernate.dialect.Oracle10gDialect", "Oracle10g");
53  //    	dialectTypes.put("org.kuali.mobility.database.entity.KMEOracleDialect", "Oracle");
54      	dialectTypes.put("org.hibernate.dialect.MySQL5Dialect", "MySQL5");
55  //    	dialectTypes.put("org.kuali.mobility.database.entity.KMEMySql5Dialect", "MySQL5");
56      	dialectTypes.put("org.hibernate.dialect.SQLServerDialect", "SQL Server");
57      	dialectTypes.put("org.hibernate.dialect.PostgreSQLDialect", "PostgreSQL");
58      	dialectTypes.put("org.hibernate.dialect.DerbyDialect", "Derby");
59  	    dialectTypes.put("org.hibernate.dialect.H2Dialect","H2");
60  //    	dialectTypes.put("", "");
61      }
62      
63      @RequestMapping(method = RequestMethod.GET)
64      public String index(Model uiModel) {
65  //    	String schema = service.getSchema();
66  //    	uiModel.addAttribute("schema", schema);
67      	DatabaseSchemaOutputForm form = new DatabaseSchemaOutputForm();
68      	uiModel.addAttribute("form", form);
69     		uiModel.addAttribute("dialectTypes", dialectTypes);
70      	return "database/schemaoutputform";
71      }
72      
73      @RequestMapping(method = RequestMethod.POST)
74      public String getSchema(Model uiModel, @ModelAttribute("form") DatabaseSchemaOutputForm form, BindingResult result) {
75  		DatabaseSchemaOutputFormValidator validator = new DatabaseSchemaOutputFormValidator();
76  		validator.validate(form, result);
77  		if (result.hasErrors()) {
78  			uiModel.addAttribute("dialectTypes", dialectTypes);
79  			return "database/schemaoutputform";
80  		} else {
81  	    	String schema = this.getSchema(form.getDialectType(), form.getDelimiter(), form.isNewLineBeforeDelimiter(), form.isRemoveForeignKeys());
82  	    	uiModel.addAttribute("schema", schema);
83  	    	uiModel.addAttribute("dialect", form.getDialectType());
84  	    	uiModel.addAttribute("delimiter", form.getDelimiter());
85  			return "database/schemaoutput";
86  		}
87      }
88  
89      @RequestMapping(method = RequestMethod.POST, params = "download")
90      public String getSchemaDownload(Model uiModel, @ModelAttribute("form") DatabaseSchemaOutputForm form, BindingResult result, HttpServletResponse response) {
91  		DatabaseSchemaOutputFormValidator validator = new DatabaseSchemaOutputFormValidator();
92  		validator.validate(form, result);
93  		if (result.hasErrors()) {
94  			uiModel.addAttribute("dialectTypes", dialectTypes);
95  			return "database/schemaoutputform";
96  		} else {
97  			try {
98  				response.setContentType("text/plain");
99  				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 }