1 /***
2 *
3 */
4 package org.telscenter.pas.authortool.main;
5
6 import java.io.File;
7 import java.io.FileNotFoundException;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 import java.net.MalformedURLException;
11
12 import javax.swing.JFileChooser;
13
14 import net.sf.sail.core.curnit.Curnit;
15 import net.sf.sail.core.curnit.CurnitArchive;
16 import net.sf.sail.core.curnit.CurnitArchiveResolver;
17 import net.sf.sail.core.curnit.CurnitFile;
18 import net.sf.sail.core.uuid.CurnitUuid;
19
20 import org.telscenter.pas.authortool.context.IAuthoringSessionManager;
21
22 /***
23 * @author turadg
24 *
25 */
26 public class SimpleFileSessionManager implements IAuthoringSessionManager {
27
28 private Curnit curnit;
29
30 boolean started;
31
32 boolean ended;
33
34 private File currentFile;
35
36 /***
37 * @param file
38 * @throws IOException
39 */
40 public SimpleFileSessionManager(final File inFile) throws IOException {
41 currentFile = inFile;
42 loadCurnit();
43 }
44
45 /***
46 * @param inFile
47 * @throws IOException
48 * @throws MalformedURLException
49 */
50 private void loadCurnit() throws IOException,
51 MalformedURLException {
52 CurnitFile curnitFile = new CurnitFile(currentFile);
53 curnit = curnitFile.getCurnit();
54
55 CurnitUuid curnitId = curnit.getCurnitId();
56 CurnitArchiveResolver.getSystemResolver().put(curnitId, currentFile.toURL());
57 }
58
59 public void endAuthoringSession() {
60 if (!started)
61 throw new IllegalStateException("session not yet started");
62 if (ended)
63 throw new IllegalStateException("session already ended");
64 ended = true;
65 }
66
67 public Curnit getCurnit() {
68 if (!started)
69 throw new IllegalStateException("session not yet started");
70 return curnit;
71 }
72
73 /***
74 * This implementation always grants extensions
75 */
76 public boolean requestExtension(int minutes) {
77 return true;
78 }
79
80 /***
81 * Save to the last place that was saved to
82 * @param curnit
83 * @throws IOException
84 */
85 public void save(Curnit curnit) throws IOException {
86 saveToFile(curnit, currentFile);
87 }
88
89 private void saveToFile(Curnit curnit, File outFile) throws FileNotFoundException, IOException {
90 CurnitArchive.writeArchive(curnit, new FileOutputStream(outFile));
91 System.out.println("wrote curnit to " + outFile);
92 }
93
94 public void saveAs(Curnit curnit) throws IOException {
95 JFileChooser chooser = new JFileChooser();;
96
97
98
99
100
101 chooser.showSaveDialog(null);
102
103
104 File file = chooser.getSelectedFile();
105
106 saveToFile(curnit, file);
107
108 System.out.println("saved");
109 }
110
111
112 /***
113 * @return
114 */
115 public static File fileForString(String fileName) {
116 if( fileName != null) {
117 if( fileName.endsWith(".zip")) {
118 return new File(fileName);
119 } else {
120 return new File(fileName + ".zip");
121 }
122
123 } else {
124 return new File("/tmp/savedCurnit.zip");
125 }
126 }
127
128 public void startAuthoringSession() {
129 if (started)
130 throw new IllegalStateException("session already started");
131 if (ended)
132 throw new IllegalStateException("session already ended");
133 started = true;
134 }
135
136
137 }