16 package net.squiz.matrix.core;
18 import net.squiz.matrix.matrixtree.*;
19 import net.squiz.matrix.debug.*;
22 import java.io.IOException;
24 import javax.swing.event.*;
25 import javax.swing.tree.*;
26 import net.squiz.matrix.ui.*;
27 import net.squiz.matrix.assetmap.*;
38 private static Map assets =
new HashMap();
40 private static Map assetTypes =
new HashMap();
42 private static String currentUserid;
43 private static String currentUserType =
"root_user";
44 private static String lastRequest =
"";
45 private static Date now =
new Date();
46 private static Date lastRequestTime =
new Date(now.getTime() - (2 * 60 * 60 * 1000));
47 private static Element lastResponse = null;
48 private static String workspaceid;
49 private static boolean isInited =
false;
50 private static int limit = 0;
52 private static EventListenerList listenerList =
new EventListenerList();
66 throw new IllegalStateException(
"Already Inited");
68 Document response = null;
70 NodeList children = response.getDocumentElement().getChildNodes();
72 for (
int i = 0; i < children.getLength(); i++) {
73 if (!(children.item(i) instanceof Element))
75 Element childElement = (Element) children.item(i);
77 if (childElement.getTagName().equals(
"asset_types")) {
78 NodeList xmlNodes = childElement.getChildNodes();
79 processAssetTypesXML(xmlNodes);
80 }
else if (childElement.getTagName().equals(
"assets")) {
81 root = processAssetsXML(childElement);
84 fireInitialisationComplete(root);
94 private static void fireInitialisationComplete(MatrixTreeNode root) {
96 Object[] listeners = listenerList.getListenerList();
97 InitialisationEvent evt = null;
101 for (
int i = listeners.length - 2; i >= 0; i -= 2) {
102 if (listeners[i] == InitialisationListener.class) {
105 evt =
new InitialisationEvent(root, root);
106 ((InitialisationListener) listeners[i + 1]).
107 initialisationComplete(evt);
117 private static void processAssetTypesXML(NodeList xmlNodes) {
119 for (
int i = 0; i < xmlNodes.getLength(); i++) {
120 if (!(xmlNodes.item(i) instanceof Element))
122 Element assetTypeElement = (Element) xmlNodes.item(i);
124 AssetType type = null;
125 AssetType parentType = null;
126 String typeCode = assetTypeElement.getAttribute(
"type_code");
127 String parentTypeCode = assetTypeElement.getAttribute(
"parent_type_code");
129 if (!(assetTypes.containsKey(parentTypeCode))) {
132 parentType =
new AssetType(parentTypeCode);
133 assetTypes.put(parentTypeCode, parentType);
138 if (!assetTypes.containsKey(typeCode)) {
139 type =
new AssetType(typeCode);
140 assetTypes.put(typeCode, type);
144 type.setInfo(assetTypeElement);
145 type.setParentType(parentType);
154 private static MatrixTreeNode processAssetsXML(Element rootElement) {
155 Node nextElement = null;
156 NodeList nodes = (NodeList) rootElement.getChildNodes();
161 nextElement = nodes.item(i++);
162 }
while (!(nextElement instanceof Element));
164 Element rootFolderElement = (Element) nextElement;
165 RootFolder parentAsset =
new RootFolder(rootFolderElement);
166 assets.put(parentAsset.getId(), parentAsset);
167 processAssetsXML(rootElement, parentAsset.getRootNode());
169 return parentAsset.getRootNode();
178 private static void processAssetsXML(Element rootElement, MatrixTreeNode parent) {
190 NodeList parentNodes = (NodeList) rootElement.getChildNodes();
192 for (
int i = 0; i < parentNodes.getLength(); i++) {
193 if (!(parentNodes.item(i) instanceof Element))
195 Element parentElement = (Element) parentNodes.item(i);
196 NodeList childNodes = (NodeList) parentElement.getChildNodes();
200 for (
int j = 0; j < childNodes.getLength(); j++) {
201 if (!(childNodes.item(j) instanceof Element))
203 Element childElement = (Element) childNodes.item(j);
204 String assetid = getIdFromElement(childElement);
205 Asset asset = loadAsset(assetid, childElement, parent, index);
208 parent.getAsset().setChildrenLoaded(
true);
213 private static Asset loadAsset(
215 Element assetElement,
216 MatrixTreeNode parent,
219 if (!assets.containsKey(assetid)) {
220 asset =
new Asset(assetElement, parent, index);
221 assets.put(assetid, asset);
225 asset.processAssetXML(assetElement);
227 asset.processAssetXML(assetElement, parent, index);
237 private static void processCurrentUserXML(Element xmlNodes) {
238 currentUserid = getIdFromElement(xmlNodes);
239 currentUserType = xmlNodes.getAttribute(
"type_code");
240 String name = xmlNodes.getAttribute(
"name");
243 public static void setLimit(
int newLimit) {
247 public static int getLimit() {
251 }
else if (Matrix.getProperty(
"parameter.asset.limit") != null) {
252 limit = Integer.parseInt(Matrix.getProperty(
"parameter.asset.limit"));
259 public static void refreshAsset(MatrixTreeNode parent, String direction,
int start,
int limit)
throws IOException {
260 String[] assetids =
new String[] { parent.getAsset().getId() };
262 processAssetsXML(element, parent);
265 public static void refreshAsset(MatrixTreeNode parent, String direction)
throws IOException {
266 refreshAsset(parent, direction, -1, -1);
269 public static int calcNextOffset(String assetid) {
271 asset.setTotalKidsLoaded(asset.getTotalKidsLoaded()+getLimit());
272 return asset.getTotalKidsLoaded();
275 public static int calcPrevOffset(String assetid) {
277 asset.setTotalKidsLoaded(asset.getTotalKidsLoaded() - getLimit());
278 return asset.getTotalKidsLoaded();
299 public static Element
makeRefreshRequest(String[] assetids, String direction,
int start,
int limit)
throws IOException {
301 int startLoc = start;
306 StringBuffer xml =
new StringBuffer(
"<command action=\"get assets\" >");
307 for (
int i = 0; i < assetids.length; i++) {
311 if (direction.equals(
"prev")) {
312 startLoc = calcPrevOffset(assetids[i]);
313 }
else if (direction.equals(
"next")) {
314 startLoc = calcNextOffset(assetids[i]);
315 }
else if (direction.equals(
"base")) {
317 asset.setTotalKidsLoaded(0);
322 startLoc = asset.getTotalKidsLoaded();
328 asset.setTotalKidsLoaded(startLoc);
335 if (assetids[i].equals(
"1")) {
336 MatrixToolkit.addAssetToXML(xml, assetids[i],
"0", startLoc, limit);
338 for (
int j=0; j< linkids.length; j++) {
339 MatrixToolkit.addAssetToXML(xml, assetids[i], linkids[j], startLoc, limit);
344 xml.append(
"</command>");
347 Date currentTime =
new Date(System.currentTimeMillis());
348 long diffResponse = (currentTime.getTime() - lastRequestTime.getTime());
349 Element currentElement = null;
350 if (lastRequest.equals(xml.toString()) && (diffResponse >= 0 && diffResponse < 2000) && lastResponse != null) {
351 currentElement = lastResponse;
355 lastRequest = xml.toString();
356 lastRequestTime =
new Date(System.currentTimeMillis());
357 lastResponse = doc.getDocumentElement();
358 currentElement = doc.getDocumentElement();
361 return currentElement;
367 public static Element
makeRefreshRequest(String[] assetids, String direction)
throws IOException {
371 public static void refreshAssets(Element element) {
372 NodeList childNodes = (NodeList) element.getChildNodes();
373 for (
int i = 0; i < childNodes.getLength(); i++) {
374 if (!(childNodes.item(i) instanceof Element))
376 Element assetElement = (Element) childNodes.item(i);
377 updateAsset(assetElement);
381 public static String[] getAllRefreshableAssetids() {
382 Iterator iterator = assets.values().iterator();
383 List assetids =
new ArrayList();
385 while (iterator.hasNext()) {
387 Iterator nodes = asset.getTreeNodes();
389 while(nodes.hasNext()) {
390 MatrixTreeNode node = (MatrixTreeNode) nodes.next();
391 if (node.getAsset().childrenLoaded()) {
392 assetids.add(node.getAsset().getId());
397 return (String[]) assetids.toArray(
new String[assetids.size()]);
400 public static void updateAsset(Element childElement) {
401 String assetid = getIdFromElement(childElement);
403 if (!assets.containsKey(assetid))
406 updateAsset(childElement, asset);
418 NodeList childNodes = (NodeList) childElement.getChildNodes();
419 parent.processAssetXML(childElement);
423 List linkids =
new ArrayList();
424 boolean hasShadowChild =
false;
431 for (
int i = 0; i < childNodes.getLength(); i++) {
433 if (!(childNodes.item(i) instanceof Element))
435 Element assetElement = (Element) childNodes.item(i);
438 String assetid = getIdFromElement(assetElement);
439 if (assetid.indexOf(
":") != -1) {
440 hasShadowChild =
true;
442 String linkid = assetElement.getAttribute(
"linkid");
443 int linkType = Integer.parseInt(assetElement.getAttribute(
"link_type"));
444 Asset asset = loadAsset(assetid, assetElement, null, index);
448 parent.propagateNode(asset, linkid, linkType, index);
449 parent.setChildrenLoaded(
true);
456 parent.removeDiffChildNodes((String[]) linkids.toArray(
new String[linkids.size()]));
459 if (!parent.
getId().equals(
"1")) {
461 Iterator nodeIterator = parent.getTreeNodes();
462 while (nodeIterator.hasNext()) {
465 refreshAsset(node,
"");
467 if (hasShadowChild) {
470 TreePath tp =
new TreePath(node.getPath());
471 if (MatrixTreeBus.getActiveTree().isExpanded(tp)) {
472 MatrixTreeBus.getActiveTree().collapsePath(tp);
473 MatrixTreeBus.getActiveTree().expandPath(tp);
479 refreshAsset((
MatrixTreeNode)MatrixTreeBus.getActiveTree().getModel().getRoot(),
"");
481 }
catch (IOException ex) {}
484 public static boolean isShadowAsset(
Asset asset) {
485 return (asset.
getId().indexOf(
":") != -1) ?
true :
false;
488 private static String getIdFromElement(Element element) {
489 return MatrixToolkit.rawUrlDecode(element.getAttribute(
"assetid"));
498 return (
Asset) assets.get(assetid);
507 return (
AssetType) assetTypes.get(typeCode);
510 public static Iterator getAssetTypes() {
511 return assetTypes.values().iterator();
514 public static String getWorkspaceid() {
518 public static Asset getCurrentUser() {
522 public static AssetType getCurrentUserType() {
526 public static MatrixTreeNode getRootFolderNode() {
539 Iterator iterator = assets.values().iterator();
540 List assets =
new ArrayList();
541 while (iterator.hasNext()) {
544 assets.add(asset.
getId());
547 return (String[]) assets.toArray(
new String[assets.size()]);
551 public static String[] getTypeCodeNames() {
552 Iterator assetTypesIterator = assetTypes.values().iterator();
553 String[] names =
new String[assetTypes.size()];
555 while (assetTypesIterator.hasNext()) {