}
if (util.isNullOrEmpty(description)) {
throw new IllegalArgumentException("'description' parameter is null or empty.");
}
UserSession userSession = userSessionManager.getUserSession(req);
if (userSession == null) {
// TODO: Throw a better Exception class here.
throw new IllegalArgumentException("No user session found.");
}
String authSubToken = userSession.getMetaData("authSubToken");
// Assignment id might be set in the JSON object if there wasn't an assignment associated
// with the embedded iframe, and the assignment was chosen at run time.
if (util.isNullOrEmpty(assignmentId)) {
assignmentId = userSession.getMetaData("assignmentId");
} else {
userSession.addMetaData("assignmentId", assignmentId);
}
Assignment assignment = assignmentDao.getAssignmentById(assignmentId);
if (assignment == null) {
throw new IllegalArgumentException(String.format(
"Could not find an assignment with id '%s'.", assignmentId));
}
AssignmentStatus status = assignment.getStatus();
if (status != AssignmentStatus.ACTIVE) {
throw new IllegalArgumentException(String.format(
"Could not add a video to a non ACTIVE assignment. "
+ "Current status of assignment id '%s' is '%s'.", assignmentId, status));
}
// Max title length is 60 characters or 100 bytes.
if (title.length() > 60) {
title = title.substring(0, 59);
}
VideoEntry newEntry = new VideoEntry();
YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
mg.setTitle(new MediaTitle());
mg.getTitle().setPlainTextContent(title);
mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, assignment.getCategory()));
mg.setKeywords(new MediaKeywords());
List<String> tags = new ArrayList<String>();
for (int i = 0; i < tagsArray.length(); i++) {
String tag = tagsArray.getString(i).trim();
mg.getKeywords().addKeyword(tag);
tags.add(tag);
}
// Sort the list of tags and join with "," so that we can easily compare
// what's in the
// datastore with what we get back from the YouTube API.
String sortedTags = util.sortedJoin(tags, ",");
mg.setDescription(new MediaDescription());
mg.getDescription().setPlainTextContent(description);
String applicationNameTag = Util.CLIENT_ID_PREFIX + SystemProperty.applicationId.get();
if (applicationNameTag.length() > 25) {
applicationNameTag = applicationNameTag.substring(0, 24) + "!";
}
mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, applicationNameTag));
// Maximum size of a developer tag is 25 characters, and we prepend 2
// characters.
if (assignmentId.length() <= 23) {
// Minimum size of a developer tag is 3 characters, so always append 2
// characters.
String assignmentIdTag = String.format("A-%s", assignmentId);
// Use a developer tag to make it easy for developers to query for all
// videos uploaded for
// a given assignment.
mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, assignmentIdTag));
} else {
log.warning(String.format("Assignment id '%s' is longer than 25 characters, and can't be "
+ "used as a developer tag.", assignmentId));
}
if (jsonObj.has("latitude") && jsonObj.has("longitude")) {
newEntry.setGeoCoordinates(new GeoRssWhere(jsonObj.getDouble("latitude"),
jsonObj.getDouble("longitude")));
} else if (!util.isNullOrEmpty(location)) {
newEntry.setLocation(location);
}
userSession.addMetaData("videoTitle", title);
userSession.addMetaData("videoDescription", description);
userSession.addMetaData("videoLocation", location);
userSession.addMetaData("phoneNumber", phoneNumber);
userSession.addMetaData("videoDate", date);
userSession.addMetaData("videoTags", sortedTags);
userSession.addMetaData("email", email);
userSessionManager.save(userSession);
youTubeApi.setAuthSubToken(authSubToken);
FormUploadToken token = youTubeApi.getFormUploadToken(newEntry);