1
2
3
4
5
6
7
8
9
10
11
12
13
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
54 dialectTypes.put("org.hibernate.dialect.MySQL5Dialect", "MySQL5");
55
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
61 }
62
63 @RequestMapping(method = RequestMethod.GET)
64 public String index(Model uiModel) {
65
66
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
106
107 throw new RuntimeException("IOError writing file to output stream");
108 }
109 return null;
110 }
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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 }