Skip to content

Commit

Permalink
Merge branch 'release/0.01'
Browse files Browse the repository at this point in the history
  • Loading branch information
pkaushik committed Mar 18, 2012
2 parents 9a6c4c5 + a92fc2a commit 57d32f1
Show file tree
Hide file tree
Showing 406 changed files with 15,944 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
@@ -0,0 +1,6 @@
*.swp
.settings
.classpath
.project
war/WEB-INF/classes/*

17 changes: 17 additions & 0 deletions README
@@ -0,0 +1,17 @@
Mi Parque version 0.01
=======================

Mi Parque is a bilingual web and smartphone application that helps residents of Chicago's Little Village stay engaged in the design of their new park via Facebook & mobile web.

Authors
--------------------

* Pallavi Anderson <<pallavi.kaushik@gmail.com>>
* Ji Lucas <<jikimlucas@gmail.com>>
* Sheila Miguez <<shekay@gmail.com>>

<a rel="license" href="http://creativecommons.org/publicdomain/zero/1.0/">
<img src="http://i.creativecommons.org/p/zero/1.0/88x31.png" style="border-style: none;" alt="CC0" />
</a>

To the extent possible under law, the authors have waived all copyright and related or neighboring rights to this work.
15 changes: 15 additions & 0 deletions src/META-INF/jdoconfig.xml
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">

<persistence-manager-factory name="transactions-optional">
<property name="javax.jdo.PersistenceManagerFactoryClass"
value="org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory"/>
<property name="javax.jdo.option.ConnectionURL" value="appengine"/>
<property name="javax.jdo.option.NontransactionalRead" value="true"/>
<property name="javax.jdo.option.NontransactionalWrite" value="true"/>
<property name="javax.jdo.option.RetainValues" value="true"/>
<property name="datanucleus.appengine.autoCreateDatastoreTxns" value="true"/>
</persistence-manager-factory>
</jdoconfig>
112 changes: 112 additions & 0 deletions src/com/miparque/restlet/ActivePollResource.java
@@ -0,0 +1,112 @@
package com.miparque.restlet;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.data.CharacterSet;
import org.restlet.data.Status;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

import com.miparque.server.ResourceNotFoundException;
import com.miparque.server.dao.ActivePoll;
import com.miparque.server.dao.Choice;
import com.miparque.server.dao.Poll;
import com.miparque.server.database.ActivePollFtDao;
import com.miparque.server.database.ChoiceFtDao;
import com.miparque.server.database.PollFtDao;

public class ActivePollResource extends ServerResource {
// pretend like we have data injection or whatever and we get different impls based on our backing data store
// and if we ever get to the point where we do any sort of testing it would be nice to be able to mock interfaces
// so we'll make these impls of interfaces, yep. unless we don't need to.
private PollFtDao pollDao = new PollFtDao();
private ChoiceFtDao choiceDao = new ChoiceFtDao();
private ActivePollFtDao activeDao = new ActivePollFtDao();

/**
* example
* {code curl http://1767.mi-parque.appspot.com/api/activepolls}
*
* @return json array of active polls and their choices
* <pre>
* [ {
* "title": "Park Name",
* "ogurl": "park-name",
* "description": "What should we name the park? Based on name submissions we are now voting on the top 3!",
* "choices": [ {
* "detail": "labor organizer, farm worker and environmental justice advocate",
* "index": "201",
* "cogurl": "601-parque-marentes",
* "choice": "Parque Marentes",
* "cogimage_url": ""
* }, ],
* "ogimage_url": "",
* "first": false,
* "multiple": false
* } ]
* </pre>
* @throws ResourceNotFoundException
* @throws JSONException
*/
@Get("json")
public Representation represent() throws ResourceNotFoundException, JSONException {
List<ActivePoll> polls = activeDao.getAll();
List<JSONObject> jsonList = new ArrayList<JSONObject>();
for (ActivePoll poll : polls) {
jsonList.add(poll.toJson());
}
JSONArray jarr = new JSONArray(jsonList);
JsonRepresentation jr = new JsonRepresentation(jarr);
jr.setCharacterSet(CharacterSet.UTF_8);
setStatus(Status.SUCCESS_OK);
return jr;
}

/**
* TODO: this does not check whether the poll is already active. it is pretty flaky.
*
* example
*
* {code curl -v -X POST -H 'content-length:0' "http://localhost:8888/api/activepoll/601"}
* @param pollId
* id of poll to activate
* @throws ResourceNotFoundException
*/
@Post()
public void activate() throws ResourceNotFoundException {
String pollId = (String) getRequest().getAttributes().get("pollId");

// TODO is it already active?

Poll poll = pollDao.get(pollId);
if (!poll.isActive()) {
throw new IllegalArgumentException("Poll is not active: " + pollId);
}
List<Choice> choices = choiceDao.getActive(pollId);
if (choices.isEmpty()) {
throw new IllegalArgumentException("Poll has no active choices: " + pollId);
}
if (choices.size() > 5) {
System.out.println("Ignoring choices after the first 5");
choices = choices.subList(0, 5);
}

ActivePoll activePoll = new ActivePoll();
activePoll.mergePoll(poll);

int i = 0;
for (Choice c : choices) {
activePoll.mergeChoice(c, i);
i++;
}
activeDao.insert(activePoll);
setStatus(Status.SUCCESS_CREATED);
}
}
50 changes: 50 additions & 0 deletions src/com/miparque/restlet/AppStatusService.java
@@ -0,0 +1,50 @@
package com.miparque.restlet;

import java.net.URLEncoder;

import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.CharacterSet;
import org.restlet.data.Status;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.service.StatusService;

import com.miparque.server.ResourceNotFoundException;

public class AppStatusService extends StatusService {

public Status getStatus(Throwable throwable, Request request, Response response) {
if (throwable instanceof ResourceNotFoundException) {
return new Status(Status.CLIENT_ERROR_NOT_FOUND, throwable);
}
if (throwable instanceof IllegalArgumentException) {
return new Status(Status.CLIENT_ERROR_BAD_REQUEST, throwable);
}
if (throwable instanceof JSONException) {
return new Status(Status.CLIENT_ERROR_BAD_REQUEST, throwable);
}
return new Status(Status.SERVER_ERROR_INTERNAL, throwable);
}

private JsonRepresentation jsonifyException(Exception e) {
JSONObject json = new JSONObject();
try {
StringBuffer sb = new StringBuffer(e.getMessage());
sb.append("\n");
for (StackTraceElement ste : e.getStackTrace()) {
sb.append(ste.toString());
sb.append("\n");
}
String msg = URLEncoder.encode(sb.toString(), "UTF-8");
json.put("error", msg);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JsonRepresentation errorJson = new JsonRepresentation(json);
errorJson.setCharacterSet(CharacterSet.UTF_8);
return errorJson;
}
}
76 changes: 76 additions & 0 deletions src/com/miparque/restlet/ChoiceResource.java
@@ -0,0 +1,76 @@
package com.miparque.restlet;

import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.data.CharacterSet;
import org.restlet.data.Status;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

import com.miparque.server.ResourceNotFoundException;
import com.miparque.server.dao.Choice;
import com.miparque.server.database.ChoiceFtDao;
import com.miparque.server.database.PollFtDao;

public class ChoiceResource extends ServerResource {
// pretend like we have data injection or whatever and we get different impls based on our backing data store
// and if we ever get to the point where we do any sort of testing it would be nice to be able to mock interfaces
// so we'll make these impls of interfaces, yep. unless we don't need to.
private PollFtDao pollDao = new PollFtDao();
private ChoiceFtDao choiceDao = new ChoiceFtDao();

/**
* Return a json representation of a poll with the choice element set to the choice specified by the og url
*
* Example:
* {code curl http://1767.mi-parque.appspot.com/api/choice/1203-person-a}
*
* <pre>
* {
* "id": "1203",
* "og_image_url": "",
* "title": "Keynotes",
* "choice": {
* "id": "1204",
* "og_image_url": "",
* "detail": "A detail",
* "index": "0",
* "choice": "Person A",
* "active": true,
* "og_url": "1203-person-a",
* "og_type": "poll"
* },
* "description": "Who should we invite to give a keynote?",
* "active": true,
* "og_type": "unassigned",
* "multiple": false,
* "og_url": "keynotes"
* }
* </pre>
*
* @param ogUrl
* the open graph url for the choice
*
* @return
* @throws ResourceNotFoundException
* @throws JSONException
*/
@Get("json")
public Representation getChoice() throws ResourceNotFoundException, JSONException {
String ogUrl = (String) getRequest().getAttributes().get("ogUrl");

Choice choice = choiceDao.getByOpenGraphUrl(ogUrl);
JSONObject choiceJson = choice.toJson();
JSONObject poll = pollDao.getJson(choice.getPollId());
poll.put("choice", choiceJson);

JsonRepresentation jr = new JsonRepresentation(poll);
jr.setCharacterSet(CharacterSet.UTF_8);

setStatus(Status.SUCCESS_OK);
return jr;
}

}
57 changes: 57 additions & 0 deletions src/com/miparque/restlet/IdeaListResource.java
@@ -0,0 +1,57 @@
package com.miparque.restlet;

import org.json.JSONArray;
import org.json.JSONException;
import org.restlet.data.CacheDirective;
import org.restlet.data.CharacterSet;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

import org.json.JSONObject;


public class IdeaListResource extends ServerResource {

@Get("json")
public Representation represent() {
// query all ideas and stuff into json array
JSONArray json = new JSONArray();

JsonRepresentation jr = new JsonRepresentation(json);
jr.setCharacterSet(CharacterSet.UTF_8);
// set cache expiry header to not cache API calls
getResponse().getCacheDirectives().add(CacheDirective.noCache());
return jr;
}

@Post("json")
public Representation acceptRepresentation(JsonRepresentation entity) {
if (!entity.getMediaType().isCompatible(MediaType.APPLICATION_JSON)) {
setStatus(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE);
return null;
}

try {
JSONObject json = entity.getJsonObject();

// create new idea with new id if all fields are valid
// persist
// query

JsonRepresentation jr = new JsonRepresentation(json);
jr.setCharacterSet(CharacterSet.UTF_8);
// set cache expiry header to not cache API calls
getResponse().getCacheDirectives().add(CacheDirective.noCache());
return jr;

} catch (JSONException e) {
setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return null;
}
}
}

0 comments on commit 57d32f1

Please sign in to comment.