Skip to content

Commit

Permalink
Cleaned up OrgNode:getOlpId function.
Browse files Browse the repository at this point in the history
  • Loading branch information
hdweiss committed Sep 19, 2012
1 parent 9ce5138 commit 6c10779
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 39 deletions.
15 changes: 15 additions & 0 deletions src/com/matburt/mobileorg/Gui/Capture/EditActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,21 @@ public void saveEdits() {
OrgUtils.announceUpdate(this);
}

public OrgNode ensureNodeStillExists(OrgNode node) {
try {
OrgNode resultNode = new OrgNode(node.id, resolver);
return node; // Everything is fine, node still exists
} catch (OrgNodeNotFoundException e) {}

// TODO Use olp path to find new node



// TODO Else return capture node

return node;
}

public OrgNode getEditedNode() {
HeadingFragment headingFragment = (HeadingFragment) getSupportFragmentManager()
.findFragmentByTag("headingFragment");
Expand Down
2 changes: 1 addition & 1 deletion src/com/matburt/mobileorg/Gui/OutlineActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void refreshTitle() {
if(this.node_id > -1) {
try {
OrgNode node = new OrgNode(this.node_id, getContentResolver());
final String subTitle = node.constructOlpId(getContentResolver()).substring("olp:".length());
final String subTitle = node.getOlpId(getContentResolver()).substring("olp:".length());
this.getSupportActionBar().setSubtitle(subTitle);
} catch (OrgNodeNotFoundException e) {
refreshDisplay();
Expand Down
73 changes: 37 additions & 36 deletions src/com/matburt/mobileorg/OrgData/OrgNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public ArrayList<String> getSiblingsStringArray(ContentResolver resolver) {
return parent.getChildrenStringArray(resolver);
}
catch (OrgNodeNotFoundException e) {
throw new IllegalArgumentException("Couln't get parent for node " + name);
throw new IllegalArgumentException("Couldn't get parent for node " + name);
}
}

Expand Down Expand Up @@ -314,55 +314,56 @@ public boolean areChildrenEditable(ContentResolver resolver) {

return true;
}


/**
* This is called when generating the olp link to the node. This path can't
* have any "[" or "]" in it's path. For example having [1/3] in the title
* will prevent org-mode from applying the edit. This method will strip that
* out of the name.
*/
private String getOlpName() {
return name.replaceAll("\\[[^\\]]*\\]", "");
}

/**
* @return The :ID: or :ORIGINAL_ID: field of the payload.
* @return The :ID:, :ORIGINAL_ID: or olp link of node.
*/
public String getNodeId(ContentResolver resolver) {
preparePayload();

String id = orgNodePayload.getId();
if(id == null)
return constructOlpId(resolver);

return id;
if(id != null && id.equals("") == false)
return id;
else
return getOlpId(resolver);
}

public String constructOlpId(ContentResolver resolver) {
public String getOlpId(ContentResolver resolver) {
StringBuilder result = new StringBuilder();
result.insert(0, name);

long currentParentId = this.parentId;
while (currentParentId > 0) {
OrgNode node;
try {
node = new OrgNode(currentParentId, resolver);
currentParentId = node.parentId;

if (currentParentId > 0)
result.insert(0, node.getOlpName() + "/");
else { // Get file nodes real name
String filename = node.getFilename(resolver);
result.insert(0, filename + ":");
}
} catch (OrgNodeNotFoundException e) {
}

ArrayList<OrgNode> nodesFromRoot;
try {
nodesFromRoot = OrgProviderUtil.getOrgNodePathFromTopLevel(
parentId, resolver);
} catch (IllegalStateException e) {
return "";
}

result.insert(0, "olp:");

if(nodesFromRoot.size() == 0)
return "";

OrgNode topNode = nodesFromRoot.get(0);
nodesFromRoot.remove(0);
result.append("olp:" + topNode.getFilename(resolver) + ":");

for(OrgNode node: nodesFromRoot)
result.append(node.getStrippedNameForOlpPathLink() + "/");

result.append(this.name);
return result.toString();
}

/**
* Olp paths containing certain symbols can't be applied by org-mode. To
* prevent node names from injecting bad symbols, we strip them out here.
*/
private String getStrippedNameForOlpPathLink() {
String result = this.name;
result = result.replaceAll("\\[[^\\]]*\\]", ""); // Strip out "[*]"
return result;
}


public String getCleanedPayload() {
preparePayload();
Expand Down
22 changes: 20 additions & 2 deletions src/com/matburt/mobileorg/OrgData/OrgProviderUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.matburt.mobileorg.OrgData;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import android.content.ContentResolver;
Expand Down Expand Up @@ -159,7 +160,25 @@ public static ArrayList<String> cursorToArrayList(Cursor cursor) {
return list;
}

static StringBuilder nodesToString(long node_id, long level, ContentResolver resolver) {
public static ArrayList<OrgNode> getOrgNodePathFromTopLevel(long node_id, ContentResolver resolver) {
ArrayList<OrgNode> nodes = new ArrayList<OrgNode>();

long currentId = node_id;
while(currentId >= 0) {
try {
OrgNode node = new OrgNode(currentId, resolver);
nodes.add(node);
currentId = node.parentId;
} catch (OrgNodeNotFoundException e) {
throw new IllegalStateException("Couldn't build entire path to root from a given node");
}
}

Collections.reverse(nodes);
return nodes;
}

public static StringBuilder nodesToString(long node_id, long level, ContentResolver resolver) {
StringBuilder result = new StringBuilder();

OrgNode node;
Expand Down Expand Up @@ -281,7 +300,6 @@ public static Cursor getFileSchedule(String filename, boolean calendarHabits, Co
Cursor cursor = resolver.query(OrgData.CONTENT_URI, OrgData.DEFAULT_COLUMNS, whereQuery,
new String[] { Long.toString(file.id) }, null);
cursor.moveToFirst();
Log.d("MobileOrg", "Found " + cursor.getCount() + " entries");
return cursor;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/src/com/matburt/mobileorg/test/OrgData/OrgNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,11 @@ public void testArchiveNodeToSiblingGeneratesEdit() {

assertEquals(baseOfEdits + 1, numberOfEdits);
}

public void testGetOlpLink() {
OrgNode node = OrgTestUtils.setupParentScenario(resolver);

String olp = node.getOlpId(resolver);
assertEquals(OrgTestUtils.setupParentScenarioChild2ChildOlpId, olp);
}
}
3 changes: 3 additions & 0 deletions tests/src/com/matburt/mobileorg/test/util/OrgTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class OrgTestUtils {
testIdPayload;
public static final String testTimestampPayloadSimple = "<" + testTimestampTimestamp + ">";


public static OrgNode getDefaultOrgNode() {
OrgNode node = new OrgNode();
node.name = "title";
Expand All @@ -52,6 +53,8 @@ public static OrgNode getComplexOrgNode() {
return node;
}


public static final String setupParentScenarioChild2ChildOlpId = "olp:" + defaultTestfilename + ":" + "child2/child2Child";
public static OrgNode setupParentScenario(ContentResolver resolver) {
OrgFile file = OrgProviderUtil.getOrCreateFile(defaultTestfilename,
defaultTestfileAlias, resolver);
Expand Down

0 comments on commit 6c10779

Please sign in to comment.