1 | /**
|
---|
2 | * CopyTree.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.agents;
|
---|
20 |
|
---|
21 | import java.util.HashMap;
|
---|
22 | import java.util.LinkedList;
|
---|
23 |
|
---|
24 | import org.expeditee.gui.Frame;
|
---|
25 | import org.expeditee.gui.FrameIO;
|
---|
26 | import org.expeditee.gui.FrameUtils;
|
---|
27 | import org.expeditee.gui.MessageBay;
|
---|
28 | import org.expeditee.items.Item;
|
---|
29 |
|
---|
30 | public class CopyTree extends TreeProcessor {
|
---|
31 |
|
---|
32 | private String _nameTo;
|
---|
33 |
|
---|
34 | private String _nameFrom;
|
---|
35 |
|
---|
36 | private String _framePath;
|
---|
37 |
|
---|
38 | private int _lastNumber = -1;
|
---|
39 |
|
---|
40 | private int _firstNumber = 1;
|
---|
41 |
|
---|
42 | private HashMap<String, String> _nameMap = new HashMap<String, String>();
|
---|
43 |
|
---|
44 | private LinkedList<String> _toReparse = new LinkedList<String>();
|
---|
45 |
|
---|
46 | public CopyTree(String framesetTo) {
|
---|
47 | _nameTo = framesetTo.trim();
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public boolean initialise(Frame init, Item launcher) {
|
---|
52 | _nameFrom = init.getFramesetName().toLowerCase();
|
---|
53 |
|
---|
54 | // create the new frameset
|
---|
55 | try {
|
---|
56 | // get the last used frame in the destination frameset
|
---|
57 | _lastNumber = FrameIO.getLastNumber(_nameTo);
|
---|
58 | Frame one = FrameIO.CreateFrameset(_nameTo, init.getPath());
|
---|
59 |
|
---|
60 | _framePath = one.getPath();
|
---|
61 | _lastNumber = -1;
|
---|
62 | _firstNumber = 1;
|
---|
63 |
|
---|
64 | // copy the original .0 frame
|
---|
65 | Frame zero = FrameIO
|
---|
66 | .LoadFrame(init.getFramesetName() + "0");
|
---|
67 | processFrame(zero);
|
---|
68 | } catch (ExistingFramesetException efe) {
|
---|
69 | MessageBay.errorMessage("A frameset called " + _nameTo
|
---|
70 | + " already exists.");
|
---|
71 | return false;
|
---|
72 | } catch (Exception e) {
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 |
|
---|
76 | return super.initialise(init, launcher);
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | protected void processFrame(Frame toProcess) {
|
---|
81 | // load a fresh copy of the frame that bypasses the cache
|
---|
82 | FrameIO.SuspendCache();
|
---|
83 |
|
---|
84 | Frame fresh = FrameIO.LoadFrame(toProcess.getName());
|
---|
85 | if (_nameMap.containsKey(fresh.getName().toLowerCase())) {
|
---|
86 | fresh
|
---|
87 | .setName(_nameMap.get(fresh.getName()
|
---|
88 | .toLowerCase()));
|
---|
89 | } else {
|
---|
90 | fresh.setFrameset(_nameTo);
|
---|
91 | fresh.setFrameNumber(++_lastNumber);
|
---|
92 |
|
---|
93 | _nameMap.put(toProcess.getName().toLowerCase(), fresh
|
---|
94 | .getName().toLowerCase());
|
---|
95 | }
|
---|
96 |
|
---|
97 | boolean added = false;
|
---|
98 | for (Item i : fresh.getSortedItems())
|
---|
99 | if (i.getLink() != null && !i.isAnnotation() && i.isLinkValid()) {
|
---|
100 | String link = i.getLink().toLowerCase();
|
---|
101 | //convert to absolute link with the old framesetName
|
---|
102 | if (FrameIO.isPositiveInteger(link)){
|
---|
103 | link = _nameFrom + link;
|
---|
104 | }
|
---|
105 | //check if we already have this in our map
|
---|
106 | if (_nameMap.containsKey(link))
|
---|
107 | link = _nameMap.get(link);
|
---|
108 | //otherwise add it to our map
|
---|
109 | else if (link.startsWith(_nameFrom)) {
|
---|
110 | _nameMap.put(link, _nameTo + (++_lastNumber));
|
---|
111 | link = "" + _lastNumber;
|
---|
112 | }
|
---|
113 | i.setLink(link);
|
---|
114 | } else if (!added && i.getLink() != null && i.isAnnotation()
|
---|
115 | && i.isLinkValid()) {
|
---|
116 | // annotation links need to be parsed at the end
|
---|
117 | if (i.getAbsoluteLink().toLowerCase().startsWith(_nameFrom)) {
|
---|
118 | _toReparse.add(fresh.getName());
|
---|
119 | added = true;
|
---|
120 | }
|
---|
121 |
|
---|
122 | }
|
---|
123 | _frameCount++;
|
---|
124 | fresh.setPath(_framePath);
|
---|
125 | FrameIO.ForceSaveFrame(fresh);
|
---|
126 | FrameIO.ResumeCache();
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Override
|
---|
130 | protected void finalise(Frame frame) {
|
---|
131 | // reparse all frames that have annotation links that may need updating
|
---|
132 | for (String name : _toReparse) {
|
---|
133 | Frame toParse = FrameIO.LoadFrame(name);
|
---|
134 | boolean changed = false;
|
---|
135 | for (Item i : toParse.getSortedItems()) {
|
---|
136 | if (i.getLink() != null && i.isAnnotation() && i.isLinkValid()) {
|
---|
137 | String link = i.getLink();
|
---|
138 | link = link.toLowerCase();
|
---|
139 | // link = link.replace(_nameFrom, _nameTo);
|
---|
140 | if (_nameMap.containsKey(link)) {
|
---|
141 | link = _nameMap.get(link);
|
---|
142 | i.setLink(link);
|
---|
143 | changed = true;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | if (changed) {
|
---|
148 | FrameIO.SaveFrame(toParse);
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | message("Tree successfully copied to " + _nameTo);
|
---|
153 | FrameUtils.DisplayFrame(_nameTo + _firstNumber);
|
---|
154 |
|
---|
155 | super.finalise(frame);
|
---|
156 | }
|
---|
157 |
|
---|
158 | }
|
---|