[919] | 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 |
|
---|
[4] | 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;
|
---|
[121] | 27 | import org.expeditee.gui.MessageBay;
|
---|
[4] | 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
|
---|
[133] | 51 | public boolean initialise(Frame init, Item launcher) {
|
---|
[4] | 52 | _nameFrom = init.getFramesetName().toLowerCase();
|
---|
| 53 |
|
---|
[8] | 54 | // create the new frameset
|
---|
| 55 | try {
|
---|
| 56 | // get the last used frame in the destination frameset
|
---|
| 57 | _lastNumber = FrameIO.getLastNumber(_nameTo);
|
---|
[1532] | 58 | Frame one = FrameIO.CreateFrameset(_nameTo, init.getPath(), null);
|
---|
[4] | 59 |
|
---|
[298] | 60 | _framePath = one.getPath();
|
---|
[8] | 61 | _lastNumber = -1;
|
---|
| 62 | _firstNumber = 1;
|
---|
[4] | 63 |
|
---|
[8] | 64 | // copy the original .0 frame
|
---|
| 65 | Frame zero = FrameIO
|
---|
[24] | 66 | .LoadFrame(init.getFramesetName() + "0");
|
---|
[8] | 67 | processFrame(zero);
|
---|
| 68 | } catch (ExistingFramesetException efe) {
|
---|
[121] | 69 | MessageBay.errorMessage("A frameset called " + _nameTo
|
---|
[23] | 70 | + " already exists.");
|
---|
| 71 | return false;
|
---|
[8] | 72 | } catch (Exception e) {
|
---|
| 73 | return false;
|
---|
[4] | 74 | }
|
---|
| 75 |
|
---|
[133] | 76 | return super.initialise(init, launcher);
|
---|
[4] | 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 |
|
---|
[80] | 84 | Frame fresh = FrameIO.LoadFrame(toProcess.getName());
|
---|
| 85 | if (_nameMap.containsKey(fresh.getName().toLowerCase())) {
|
---|
[4] | 86 | fresh
|
---|
[80] | 87 | .setName(_nameMap.get(fresh.getName()
|
---|
[4] | 88 | .toLowerCase()));
|
---|
| 89 | } else {
|
---|
| 90 | fresh.setFrameset(_nameTo);
|
---|
| 91 | fresh.setFrameNumber(++_lastNumber);
|
---|
| 92 |
|
---|
[80] | 93 | _nameMap.put(toProcess.getName().toLowerCase(), fresh
|
---|
| 94 | .getName().toLowerCase());
|
---|
[4] | 95 | }
|
---|
| 96 |
|
---|
| 97 | boolean added = false;
|
---|
[1415] | 98 | for (Item i : fresh.getSortedItems())
|
---|
[4] | 99 | if (i.getLink() != null && !i.isAnnotation() && i.isLinkValid()) {
|
---|
[23] | 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
|
---|
[4] | 106 | if (_nameMap.containsKey(link))
|
---|
| 107 | link = _nameMap.get(link);
|
---|
[23] | 108 | //otherwise add it to our map
|
---|
[4] | 109 | else if (link.startsWith(_nameFrom)) {
|
---|
| 110 | _nameMap.put(link, _nameTo + (++_lastNumber));
|
---|
[23] | 111 | link = "" + _lastNumber;
|
---|
[4] | 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
|
---|
[23] | 117 | if (i.getAbsoluteLink().toLowerCase().startsWith(_nameFrom)) {
|
---|
[80] | 118 | _toReparse.add(fresh.getName());
|
---|
[4] | 119 | added = true;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | }
|
---|
[45] | 123 | _frameCount++;
|
---|
[298] | 124 | fresh.setPath(_framePath);
|
---|
[23] | 125 | FrameIO.ForceSaveFrame(fresh);
|
---|
[4] | 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;
|
---|
[1415] | 135 | for (Item i : toParse.getSortedItems()) {
|
---|
[4] | 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 | }
|
---|
[7] | 147 | if (changed) {
|
---|
[4] | 148 | FrameIO.SaveFrame(toParse);
|
---|
[7] | 149 | }
|
---|
[4] | 150 | }
|
---|
| 151 |
|
---|
| 152 | message("Tree successfully copied to " + _nameTo);
|
---|
[24] | 153 | FrameUtils.DisplayFrame(_nameTo + _firstNumber);
|
---|
[45] | 154 |
|
---|
| 155 | super.finalise(frame);
|
---|
[4] | 156 | }
|
---|
| 157 |
|
---|
| 158 | }
|
---|