24
24
25
25
package io .github .todolist .core .domain ;
26
26
27
- import javax .persistence .*;
27
+ import java .io .BufferedReader ;
28
+ import java .io .BufferedWriter ;
29
+ import java .io .File ;
30
+ import java .io .FileWriter ;
31
+ import java .io .InputStreamReader ;
28
32
import java .io .Serializable ;
33
+ import java .nio .file .Path ;
29
34
import java .util .Date ;
30
35
36
+ import javax .persistence .Column ;
37
+ import javax .persistence .Entity ;
38
+ import javax .persistence .EnumType ;
39
+ import javax .persistence .Enumerated ;
40
+ import javax .persistence .GeneratedValue ;
41
+ import javax .persistence .Id ;
42
+ import javax .persistence .NamedQueries ;
43
+ import javax .persistence .NamedQuery ;
44
+ import javax .persistence .Temporal ;
45
+ import javax .persistence .TemporalType ;
46
+
31
47
/**
32
48
* Todo entity.
33
49
*
40
56
@ NamedQuery (name = "findTodosByTitle" , query = "SELECT t FROM Todo t where t.userId = ?1 and upper(t.title) like ?2 order by t.dueDate" )
41
57
})
42
58
public class Todo implements Serializable {
43
-
59
+
60
+ // If the JAVA_HOME isn't set, use the Heroku Java location
61
+ static final String NATIVE2ASCII = System .getProperty ("JAVA_HOME" , "./.jdk" ) + File .separator + "bin" + File .separator + "native2ascii" ;
44
62
@ Id
45
63
@ GeneratedValue
46
64
private long id ;
@@ -64,12 +82,44 @@ public Todo() {
64
82
65
83
public Todo (long userId , String title , boolean done , Priority priority , Date dueDate ) {
66
84
this .userId = userId ;
85
+
86
+ if (title != null )
87
+ title = native2ascii (title );
88
+
67
89
this .title = title ;
68
90
this .done = done ;
69
91
this .priority = priority ;
70
92
this .dueDate = dueDate ;
71
93
}
72
94
95
+ private static BufferedReader getOutput (Process p ) {
96
+ return new BufferedReader (new InputStreamReader (p .getInputStream ()));
97
+ }
98
+
99
+ private String native2ascii (String title ) {
100
+ System .out .println ("Running: " + NATIVE2ASCII );
101
+ try {
102
+
103
+ BufferedWriter writer = new BufferedWriter (new FileWriter ("title.txt" ));
104
+ writer .write (title );
105
+ writer .close ();
106
+ Process p = Runtime .getRuntime ().exec (NATIVE2ASCII + " title.txt" );
107
+ BufferedReader output = getOutput (p );
108
+ String line = "" ;
109
+
110
+ while ((line = output .readLine ()) != null ) {
111
+ if (!title .equals (line ))
112
+ System .out .println ("Found non-ascii title. Converted from '" + title + "' to '" + line + "'" );
113
+ title = line ;
114
+ }
115
+
116
+ } catch (Exception e ) {
117
+ // if an error occurs, send back the original title
118
+ e .printStackTrace ();
119
+ }
120
+ return title ;
121
+ }
122
+
73
123
public long getId () {
74
124
return id ;
75
125
}
@@ -87,7 +137,7 @@ public String getTitle() {
87
137
}
88
138
89
139
public void setTitle (String title ) {
90
- this .title = title ;
140
+ this .title = native2ascii ( title ) ;
91
141
}
92
142
93
143
public boolean isDone () {
0 commit comments