root/other-projects/nz-flag-design/trunk/main-form/my-design.jsp @ 29917

Revision 29917, 9.3 KB (checked in by davidb, 4 years ago)

JSP page for saving and retrieving published flags

  • Property svn:executable set to *
Line 
1<%@ page contentType="text/html; charset=UTF-8" %>
2<!DOCTYPE html> 
3<html id="story">
4  <head>
5    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
6    <meta name="viewport" content="width=device-width, initial-scale=1"/>
7    <title>NZ Flag Design @ The University of Waikato</title>
8   
9    <script src="css/source-sans-pro.js"></script>
10    <link href="css/styles.css" rel="stylesheet"/>
11    <link href="css/storystyle.css" rel="stylesheet"/>
12   
13   
14    <link rel="stylesheet"  href="lib-slider/css/jquery.mobile-1.3.0.css"/>
15    <link rel="stylesheet" href="lib-slider/css/jqm-demos.css"/>
16    <link rel="stylesheet" href="lib-slider/css/swipe-page.css"/>
17    <link rel="shortcut icon" href="lib-slider/favicon.ico"/>       
18
19    <script src="lib/jquery-1.8.2.min.js"></script>
20   
21    <script src="lib-slider/js/jquery.mobile.demos.js"></script>
22    <script src="lib-slider/js/jquery.mobile-1.3.0.js"></script>
23
24
25
26  <%@ page import="java.io.*,java.util.*,java.awt.image.*,javax.imageio.*,javax.xml.bind.DatatypeConverter,java.nio.file.Files,org.json.*" %>
27
28  <%!
29     public static void copyFile( File from, File to ) throws IOException {
30       Files.copy( from.toPath(), to.toPath() );
31     }
32
33     public static void publishFile(File myFlagFile, File pubFlagFile)
34     {
35       try {
36         copyFile(myFlagFile,pubFlagFile);
37       }
38       catch (Exception e) {
39         e.printStackTrace();
40       }
41     }
42
43     public String publishFlag(HttpServletRequest request)
44     {
45       String doc_base = getServletContext().getRealPath("/");
46       File render_dir = new File(doc_base,"render-3d");
47       File render_images_dir = new File(render_dir,"images");
48
49       String png_filename = null;
50       String svg_filename = null;
51
52       String myid = request.getParameter("myid");
53       if (myid == null) {
54      png_filename = "flag.png";
55      svg_filename = "flag.svg";
56       }
57       else {     
58      png_filename = "flag-" + myid + ".png";
59      svg_filename = "flag-" + myid + ".svg";
60       }
61     
62       File full_my_png_file  = new File(render_images_dir,png_filename);
63       File full_my_svg_file  = new File(render_images_dir,svg_filename);
64
65       File pub_dir = new File(doc_base,"published");
66       String pub_name = request.getParameter("pub-name");
67       if (pub_name == null) {
68         pub_name = "tmp";
69       }
70
71       File my_pub_dir = new File(pub_dir,pub_name);
72       if (!my_pub_dir.exists()) {
73         my_pub_dir.mkdir();
74       }
75       File full_pub_my_png_file = new File(my_pub_dir,"myflag.png");
76       File full_pub_my_svg_file = new File(my_pub_dir,"myflag.svg");
77
78       publishFile(full_my_png_file,full_pub_my_png_file);
79       publishFile(full_my_svg_file,full_pub_my_svg_file);
80
81       return pub_name;
82     }
83
84
85     void savePubInfo(HttpServletRequest request, String pub_name)
86     {
87       String doc_base = getServletContext().getRealPath("/");
88       File pub_dir = new File(doc_base,"published");
89       File my_pub_dir = new File(pub_dir,pub_name);
90
91       File full_pub_json_file = new File(my_pub_dir,"pubinfo.json");
92
93       try {
94         String name  = request.getParameter("name");
95         String email = request.getParameter("email");
96         String desc  = request.getParameter("desc");
97
98     BufferedWriter bw = new BufferedWriter(new FileWriter(full_pub_json_file));
99         bw.write("{ \"name\": \"" + name + "\", \"email\": \"" + email + "\", \"desc\": \"" + desc + "\"}");
100         bw.close();
101       }
102       catch (Exception e) {
103         e.printStackTrace();
104       }
105     }
106
107   %>
108
109
110  <%
111     
112     String action = request.getParameter("action");
113
114     String my_flag_png = "myflag.png";
115     String my_flag_svg = "myflag.svg";
116
117     String pub_name = null;
118
119     String name  = null;
120     String email = null;
121     String desc  = null;
122
123
124     if ((action != null) && action.equals("publish")) {
125       pub_name = publishFlag(request);
126       savePubInfo(request,pub_name);
127       name  = request.getParameter("name");
128       email = request.getParameter("email");
129       desc  = request.getParameter("desc");
130     }
131     else {
132       // read in JSON file and get details
133       // set them up as Java variables for producing the page through Javascript
134
135       pub_name = request.getParameter("pub-name");
136       if (pub_name == null) {
137         pub_name = "tmp";
138       }
139
140       String doc_base = getServletContext().getRealPath("/");
141       File pub_dir = new File(doc_base,"published");
142       File my_pub_dir = new File(pub_dir,pub_name);       
143       File full_pub_json_file = new File(my_pub_dir,"pubinfo.json");
144
145       String everything = null;
146       BufferedReader br = new BufferedReader(new FileReader(full_pub_json_file));
147       try {
148
149         StringBuilder sb = new StringBuilder();
150         String line = br.readLine();
151
152         while (line != null) {
153            sb.append(line);
154            sb.append(System.lineSeparator());
155            line = br.readLine();
156         }
157         everything = sb.toString();
158       }
159       catch (Exception e) {
160         e.printStackTrace();
161       }
162       finally {
163          br.close();
164       }
165
166       JSONObject obj = new JSONObject(everything);
167       name  = obj.getString("name");
168       email = obj.getString("email");
169       desc  = obj.getString("desc");
170     }
171
172     
173   %>
174
175    <style>
176      .vertical-align {
177        position: relative;
178        top: 50%;
179        -webkit-transform: translateY(-50%);
180        -ms-transform: translateY(-50%);
181        transform: translateY(-50%);
182      }
183    </style>
184
185  </head>
186
187  <body>
188    <div data-role="page" id="innovation-story-01" class="demo-page">
189     
190      <div data-role="content">
191     
192    <a target="_parent" href="../index.html" class="back-button back-left"></a>
193                                         
194    <div class="story-page" id="static-info-page">
195      <!-- put custom content here -->
196     
197      <div class="story-title" style="margin-left: 25px;">
198
199        <h1 class="center">My Design @ The University of Waikato</h1>
200
201        <p class="center" style="margin: 6px;">-- The New Zealand Flag Debate</p>
202       
203        <div class="center" style="height: 220px; position: relative;">
204          <div style="display: inline-block; position: relative;">
205        <img alt="" src="//upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Flag_of_New_Zealand.svg/250px-Flag_of_New_Zealand.svg.png" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Flag_of_New_Zealand.svg/375px-Flag_of_New_Zealand.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Flag_of_New_Zealand.svg/500px-Flag_of_New_Zealand.svg.png 2x" data-file-width="1200" data-file-height="600" height="200" widthXX="350" />
206          </div>
207          <div style="height: 200px; font-size: 300%; display: inline-block;" class="vertical-alignXX">VS</div>
208          <div style="display: inline-block; position: relative">
209        <a id="myflag-svg" data-ajax="false" ><img id="myflag-png" height="200" style="border: none;"/></a>
210          </div>
211        </div>
212
213
214
215<script type="text/javascript">
216
217var published_id = "<%= request.getParameter("pub-name") %>";
218var name  = "<%= name %>";
219var email = "<%= email %>";
220var desc  = "<%= desc %>";
221
222document.write("<center>");
223document.write("  <div style=\"width: 500px; background: #181818; padding: 5px; margin: 10px;\">");
224document.write("    <p>Designer: " + name + "<p/>");
225document.write("    <p>Email: " + email + "<p/>");
226document.write("    <p>Description: " + desc + "<p/>");
227document.write("  </div>");
228document.write("</center>");
229
230
231var a2a_config = a2a_config || {};
232a2a_config.linkname = "NZ Flag Design @ University of Waikato";
233
234var base_url = location.protocol + '//' + location.host + location.pathname;
235
236a2a_config.linkurl = base_url + "?pub-name=" + published_id
237
238</script>
239
240<!-- AddToAny BEGIN -->
241<center>
242<div class="a2a_kit a2a_kit_size_32 a2a_default_style" style="width: 260px;">
243<a class="a2a_dd" href="https://www.addtoany.com/share_save"></a>
244<a class="a2a_button_email"></a>
245<a class="a2a_button_facebook"></a>
246<a class="a2a_button_google_plus"></a>
247<a class="a2a_button_google_gmail"></a>
248<a class="a2a_button_twitter"></a>
249<a class="a2a_button_tumblr"></a>
250</div>
251</center>
252
253<script type="text/javascript" src="//static.addtoany.com/menu/page.js"></script>
254<!-- AddToAny END -->
255
256
257    <center>
258    <p style="font-size: 200%;">
259      <i>
260      <a href="../index.html">Interactive Flag Design Website</a> by:
261       <a target="_blank" href="http://cosi.cms.waikato.ac.nz">The Centre for Open Source Innovation</a>
262       <a target="_blank" href="http://cosi.cms.waikato.ac.nz">(COSI)</a> at the <a href="http://www.waikato.ac.nz">University of Waikato</a>   
263     </i>
264    </p>   
265    </center>
266
267
268      </div>
269      <!-- end of putting custom content -->
270
271    </div> <!-- end story-page-->       
272   
273      </div><!-- /content -->
274     
275    </div><!-- /page -->
276
277    <script>
278      $(document).ready( function() {
279     
280      var my_flag_png = "<%= "../published/" + pub_name + "/" + my_flag_png %>";
281      var my_flag_svg = "<%= "../published/" + pub_name + "/" + my_flag_svg %>";
282
283      $('#myflag-png').attr("src",  my_flag_png);
284      $('#myflag-svg').attr("href", my_flag_svg);
285
286      });
287
288    </script>
289
290  </body>
291</html>
292 
293
294
295
296
297
Note: See TracBrowser for help on using the browser.