1 | package org.honours.greenstone;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import org.apache.log4j.Logger;
|
---|
6 | import org.expeditee.items.Item;
|
---|
7 | import org.expeditee.items.Picture;
|
---|
8 | import org.expeditee.items.Text;
|
---|
9 | import org.honours.collection.CollectionItem;
|
---|
10 | import org.honours.collection.NoteLayer;
|
---|
11 |
|
---|
12 | public class CollectionContentEditor {
|
---|
13 |
|
---|
14 | static Logger logger = Logger.getLogger(org.honours.greenstone.CollectionContentEditor.class.getName());
|
---|
15 |
|
---|
16 | private CollectionItem _collectionItem;
|
---|
17 | private ArchiveFileEditor _archiveFileEditor;
|
---|
18 | //private HtmlForDocXML _htmlForDocXML;
|
---|
19 |
|
---|
20 | public CollectionContentEditor(CollectionItem currCI, String _collectionPath) {
|
---|
21 | _collectionItem = currCI;
|
---|
22 | _archiveFileEditor = new ArchiveFileEditor(_collectionItem.getDocXML());
|
---|
23 | }
|
---|
24 |
|
---|
25 | public void editCollectionItems() {
|
---|
26 |
|
---|
27 | List<Item> items = _collectionItem.getFrame().getItems();
|
---|
28 | boolean noChange = true;
|
---|
29 | ArchiveFileReader afr = new ArchiveFileReader(_collectionItem.getDocXML());
|
---|
30 |
|
---|
31 | for(Item item : items){
|
---|
32 |
|
---|
33 | if(item instanceof Text){
|
---|
34 | if(item.isAnnotation()){
|
---|
35 | //TODO: Deal with annotation items correctly.
|
---|
36 |
|
---|
37 | }else{
|
---|
38 |
|
---|
39 | Text text = (Text)item;
|
---|
40 |
|
---|
41 | if(text.getData() != null)
|
---|
42 | if(text.getData().size() > 0){
|
---|
43 | String elemText = afr.obtainElementText(text.getData().get(0));
|
---|
44 |
|
---|
45 | if(!elemText.equals(null)){
|
---|
46 | if(elemText.equals(text.getText()))
|
---|
47 | continue;
|
---|
48 | else
|
---|
49 | noChange = false;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | if(!noChange)
|
---|
54 | _archiveFileEditor.editArchiveDocFile(text);
|
---|
55 |
|
---|
56 | }
|
---|
57 | }else if(item instanceof Picture){
|
---|
58 | //TODO: Deal with picture items correctly.
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | noChange = writeFrameID(afr,noChange);
|
---|
63 | noChange = addNotes();
|
---|
64 | //addNotes();
|
---|
65 |
|
---|
66 | if(!noChange)
|
---|
67 | _archiveFileEditor.write();
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Method for adding/editing the framID metadata value
|
---|
72 | * @param afr
|
---|
73 | * @param noChange
|
---|
74 | */
|
---|
75 | private boolean writeFrameID(ArchiveFileReader afr, boolean noChange){
|
---|
76 |
|
---|
77 | //Write code to write frame number.
|
---|
78 | String frameID = afr.obtainElementText("frameID");
|
---|
79 | String currentFrameID = Integer.toString(_collectionItem.getFrame().getNumber());
|
---|
80 | Text frameIDtext = null;
|
---|
81 |
|
---|
82 | if(frameID == null){
|
---|
83 | //create a new frameID element
|
---|
84 | frameIDtext = new Text(currentFrameID);
|
---|
85 | frameIDtext.setData("frameID");
|
---|
86 |
|
---|
87 | }else{
|
---|
88 | //check if frameID is the same as the current frame name/number.
|
---|
89 | if(!frameID.equals(currentFrameID)){
|
---|
90 |
|
---|
91 | frameIDtext = new Text(currentFrameID);
|
---|
92 | frameIDtext.setData("frameID");
|
---|
93 |
|
---|
94 | }
|
---|
95 |
|
---|
96 | }
|
---|
97 |
|
---|
98 | if(frameIDtext != null){
|
---|
99 | System.err.println("Setting frame metadata... " + frameIDtext.getText());
|
---|
100 | _archiveFileEditor.editArchiveDocFile(frameIDtext);
|
---|
101 | noChange = false;
|
---|
102 | }
|
---|
103 |
|
---|
104 | return noChange;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Method to output any notes associated with an item to the
|
---|
109 | * doc.xml file.
|
---|
110 | */
|
---|
111 | private boolean addNotes() {
|
---|
112 | //Add notes to docXML file.
|
---|
113 | NoteLayer noteLayer = _collectionItem.getNoteLayer();
|
---|
114 |
|
---|
115 | if(noteLayer != null){
|
---|
116 | StringBuffer notesStringBuffer = new StringBuffer("");
|
---|
117 |
|
---|
118 | notesStringBuffer.append("<table attr=\"norect\" cellpadding=\"0\" width=\"100%\">");
|
---|
119 | notesStringBuffer.append("<tbody attr=\"norect\">");
|
---|
120 |
|
---|
121 | for(Text t : noteLayer.getFrame().getTextItems()){
|
---|
122 | notesStringBuffer.append("<tr attr=\"norect\">");
|
---|
123 | notesStringBuffer.append("<td attr=\"norect\">");
|
---|
124 |
|
---|
125 | notesStringBuffer.append(t.getText() + "<br/>");
|
---|
126 |
|
---|
127 | notesStringBuffer.append("</td>");
|
---|
128 | notesStringBuffer.append("</tr>");
|
---|
129 | }
|
---|
130 |
|
---|
131 | notesStringBuffer.append("</tbody>");
|
---|
132 | notesStringBuffer.append("</table>");
|
---|
133 |
|
---|
134 | if(!notesStringBuffer.toString().equals(""))
|
---|
135 | {
|
---|
136 | //Write a "Notes" metadata element to the doc.xml file.
|
---|
137 | Text notesText = new Text(notesStringBuffer.toString());
|
---|
138 | notesText.setData("Notes");
|
---|
139 | _archiveFileEditor.editArchiveDocFile(notesText);
|
---|
140 | }
|
---|
141 |
|
---|
142 | System.err.println("Writing out notes...");
|
---|
143 | return false; //false equates to a change.
|
---|
144 | }
|
---|
145 |
|
---|
146 | return true; //true equates to "no change"
|
---|
147 | }
|
---|
148 |
|
---|
149 | }
|
---|