Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
MatrixTreeComm.java
1 
16  /*
17  * :tabSize=4:indentSize=4:noTabs=false:
18  * :folding=explicit:collapseFolds=1:
19  */
20 
21 package net.squiz.matrix.core;
22 
23 import net.squiz.matrix.matrixtree.*;
24 import net.squiz.matrix.assetmap.*;
25 import org.w3c.dom.*;
26 import java.io.IOException;
27 import javax.swing.SwingUtilities;
28 import net.squiz.matrix.ui.*;
29 import net.squiz.matrix.debug.*;
30 import java.net.MalformedURLException;
31 
36 public class MatrixTreeComm implements NewLinkListener, NewAssetListener {
37 
38  // {{{ Public Methods
39 
45  public void requestForNewLink(final NewLinkEvent evt) {
46  createLink(
47  evt.getType(),
48  evt.getSourceNodes(),
49  evt.getParentNode(),
50  evt.getIndex(),
51  evt.getPrevIndex(),
52  evt.getParentIds()
53  );
54  }
55 
61  public void requestForNewAsset(NewAssetEvent evt) {
62  String typeCode = evt.getTypeCode();
63  MatrixTreeNode parent = evt.getParentNode();
64  int index = evt.getIndex();
65  String parentAssetid = MatrixToolkit.rawUrlEncode(parent.getAsset().getId(), true);
66 
67  if (index >= 0) {
68  if (parent.getChildCount() > 0) {
69  int modifier = 0;
70  if (index >= parent.getChildCount()) {
71  index = parent.getChildCount()-1;
72  modifier = 1;
73  }
74  index = (((MatrixTreeNode)parent.getChildAt(index)).getSortOrder())+modifier;
75  }
76  }
77 
78  String xml = "<command action=\"get url\" cmd=\"add\" " +
79  "parent_assetid=\"" + parentAssetid +
80  "\" pos=\"" + index + "\" type_code=\"" +
81  typeCode + "\" />";
82 
83  Document response = null;
84  MatrixStatusBar.setStatus(Matrix.translate("asset_map_status_bar_requesting"));
85 
86  try {
87  response = Matrix.doRequest(xml);
88  } catch (IOException ioe) {
89  Object[] transArgs = { ioe.getMessage() };
90  GUIUtilities.error(Matrix.translate("asset_map_error_request_failed", transArgs), Matrix.translate("asset_map_dialog_title_error"));
91  MatrixStatusBar.setStatusAndClear(Matrix.translate("asset_map_error_request_failed"), 1000);
92  Log.log("Request for new Asset failed", MatrixTreeComm.class, ioe);
93  return;
94  }
95 
96  MatrixStatusBar.setStatusAndClear(Matrix.translate("asset_map_status_bar_success"), 1000);
97  NodeList children = response.getDocumentElement().getChildNodes();
98  String url = null;
99  for (int i = 0; i < children.getLength(); i++) {
100  if (!(children.item(i) instanceof Element))
101  continue;
102  Element element = (Element) children.item(i);
103  if (element.getTagName().equals("url")) {
104  url = element.getFirstChild().getNodeValue();
105  }
106  }
107  try {
108  AssetMap.getURL(url);
109  } catch (MalformedURLException mue) {
110  Log.log("Could not load new asset interface in right pane", MatrixTreeComm.class, mue);
111  }
112  }
113 
126  public static void createLink(
127  final String linkType,
128  final MatrixTreeNode[] children,
129  MatrixTreeNode _parent,
130  int _index,
131  final int prevIndex,
132  final String[] parentIds) {
133 
134  // Make sure ExpandingNode is not our parent
135  if (_parent instanceof ExpandingNode) {
136  if (_parent instanceof ExpandingNextNode) {
137  _index = AssetManager.getLimit();
138  } else {
139  _index = -1;
140  }
141  _parent = (MatrixTreeNode)_parent.getParent();
142  }
143 
144  final int index = _index;
145  final MatrixTreeNode parent = _parent;
146 
147  final String[] assetids = new String[] { parent.getAsset().getId() };
148  AssetRefreshWorker worker = new AssetRefreshWorker(assetids, true) {
149  public Object construct() {
150  try {
151  int newIndex = index;
152  if (!parent.getAsset().getId().equals("1")) {
153  int limit = AssetManager.getLimit();
154  // we need to change the index since we are not on the first set
155  if (index >= limit) {
156  // move the asset to the next set
157  int modifier = 0;
158  // check for previous node
159  if (parent.getAsset().getTotalKidsLoaded() == 0) {
160  modifier = 1;
161  }
162  newIndex = ((MatrixTreeNode)parent.getChildAt(limit-modifier)).getSortOrder() + children.length;
163 
164  } else if ((index <= 0) && (parent.getAsset().getTotalKidsLoaded() > 0)) {
165  if (parent.getChildCount() > 0) {
166  // move the asset to previous set
167  newIndex = ((MatrixTreeNode)parent.getChildAt(1)).getSortOrder() - children.length;
168  }
169  } else if (index >= 0) {
170  if (parent.getChildCount() > index) {
171  newIndex = ((MatrixTreeNode)parent.getChildAt(index)).getSortOrder();
172  } else if (parent.getChildCount() > 0) {
173  newIndex = ((MatrixTreeNode)parent.getChildAt(parent.getChildCount()-1)).getSortOrder()+1;
174  }
175  }
176  }
177 
178  Boolean refresh = doLinkRequest(
179  linkType,
180  children,
181  parent,
182  newIndex,
183  parentIds
184  );
185  if (refresh == Boolean.TRUE) {
186  String parentid = parent.getAsset().getId();
187  return AssetManager.makeRefreshRequest(assetids, "");
188  }
189 
190  } catch (IOException ioe) {
191  return ioe;
192  }
193  return null;
194  }
195  public void finished() {
196  // if we need to refresh the parent, call super to do so
197  if (get() != null)
198  super.finished();
199  else
200  MatrixStatusBar.setStatusAndClear(Matrix.translate("asset_map_status_bar_success"), 1000);
201  }
202  };
203  worker.start();
204  }
205 
206  // }}}
207  // {{{ Private Methods
208 
225  private static String generateCreateLinkXML(
226  String linkType,
227  String toParentId,
228  MatrixTreeNode[] children,
229  int index,
230  String[] parentIds) {
231  StringBuffer xml = new StringBuffer();
232  xml.append("<command action=\"").append(linkType).append("\"");
233  xml.append(" to_parent_assetid=\"").append(toParentId).append("\"");
234  xml.append(" to_parent_pos=\"").append(index).append("\">");
235 
236  for (int i = 0; i < children.length; i++) {
237  String assetid = MatrixToolkit.rawUrlEncode(children[i].getAsset().getId(), true);
238  String linkid = children[i].getLinkid();
239  String parentid = null;
240  try {
241  parentid = MatrixToolkit.rawUrlEncode(
242  ((MatrixTreeNode) children[i].getParent()).getAsset().getId(),
243  true
244  );
245  } catch (NullPointerException ex) {
246  parentid = parentIds[i];
247  }
248  xml.append("<asset assetid=\"").append(assetid).append("\" ");
249  xml.append(" linkid=\"").append(linkid).append("\" ");
250  xml.append(" parentid=\"").append(parentid).append("\" />");
251  }
252  xml.append("</command>");
253 
254  return xml.toString();
255  }
256 
266  private static Boolean doLinkRequest(
267  String linkType,
268  MatrixTreeNode[] children,
269  MatrixTreeNode parent,
270  int index,
271  String[] parentIds) throws IOException {
272 
273  // Cue tree defined a move to an unexpaned folder as index -1
274  // so convert this to 0 for matrix
275  if (index == -1)
276  index = 0;
277 
278  String toParentId = MatrixToolkit.rawUrlEncode(parent.getAsset().getId(), true);
279  String xml = generateCreateLinkXML(linkType, toParentId, children, index, parentIds);
280  Document response = Matrix.doRequest(xml);
281  NodeList childNodes = response.getDocumentElement().getChildNodes();
282 
283  String url = null;
284  Boolean refresh = Boolean.FALSE;
285 
286  for (int i = 0; i < childNodes.getLength(); i++) {
287  if (!(childNodes.item(i) instanceof Element))
288  continue;
289  Element element = (Element) childNodes.item(i);
290  if (element.getTagName().equals("url")) {
291  url = element.getFirstChild().getNodeValue();
292  } else if (element.getTagName().equals("success")) {
293  refresh = Boolean.TRUE;
294  }
295  }
296 
297  // if there was a url, we need to start a hipo to move the nodes
298  // there were not on the same branch
299  if (url != null)
300  AssetMap.openWindow(url, "");
301 
302  return refresh;
303  }
304 }
305