public static org.sakaiproject.content.api.ContentResource getResource(java.lang.String param0) throws org.sakaiproject.exception.PermissionException, org.sakaiproject.exception.IdUnusedException, org.sakaiproject.exception.TypeException { org.sakaiproject.content.api.ContentHostingService service = getInstance(); if (service == null) return null; return service.getResource(param0); }
private void addFolder(ContentCollection n, ResourceDefinition rp, int depth) throws SDataException { put("available", n.isAvailable()); put("hidden", n.isHidden()); if (!n.isHidden()) { Time releaseDate = n.getReleaseDate(); if (releaseDate != null) { put("releaseDate", releaseDate.getTime()); } Time retractDate = n.getRetractDate(); if (retractDate != null) { put("retractDate", retractDate.getTime()); } } ContentCollection cc = (ContentCollection) n; put("members", cc.getMemberCount()); if (depth >= 0) { Map<String, Object> nodes = new HashMap<String, Object>(); // list of IDs List<?> l = cc.getMembers(); int i = 0; for (int k = 0; k < l.size(); k++) { String memberID = (String) l.get(k); ContentEntity cn = null; try { cn = contentHostingService.getResource(memberID); } catch (Exception idex) { try { String collectionPath = memberID; if (!collectionPath.endsWith("/")) { collectionPath = collectionPath + "/"; } cn = contentHostingService.getCollection(collectionPath); } catch (Exception ex) { } } if (cn != null) { try { Map<String, Object> m = new CHSNodeMap(cn, depth, rp); m.put("position", String.valueOf(i)); nodes.put(getName(cn), m); } catch (SDataAccessException sdae) { // hide the item from the list continue; } } i++; } put("nitems", nodes.size()); put("items", nodes); } }
public InputStream getInputStream(PaperFile paperFile) { PaperFileImpl impl = castToImpl(paperFile); String path = impl.getPath(); try { ContentResource resource = contentHostingService.getResource(path); return resource.streamContent(); } catch (Exception e) { LOG.info("Failed to get stream for: " + path); } return null; }
public RankImage createRankImageAttachmentObject(String attachId, String name) { if (!isRanksEnabled()) { LOG.warn("createRankImageAttachmentObject invoked, but ranks are disabled"); return null; } try { RankImage attach = new RankImageImpl(); attach.setCreated(new Date()); attach.setModified(new Date()); ContentResource cr = contentHostingService.getResource(attachId); User creator = userDirectoryService.getUser( cr.getProperties().getProperty(cr.getProperties().getNamePropCreator())); attach.setCreatedBy(creator.getDisplayName()); User modifier = userDirectoryService.getUser( cr.getProperties().getProperty(cr.getProperties().getNamePropModifiedBy())); attach.setModifiedBy(modifier.getDisplayName()); attach.setAttachmentSize((Long.valueOf(cr.getContentLength())).toString()); attach.setAttachmentId(attachId); attach.setAttachmentName(name); attach.setAttachmentType(cr.getContentType()); String tempString = cr.getUrl(); attach.setAttachmentUrl(resourceUrlEscaping(tempString)); getHibernateTemplate().saveOrUpdate(attach); return attach; } catch (Exception e) { e.printStackTrace(); return null; } }
public String processCreate() { try { if (!this.checkAccess()) { throw new PermissionException( SessionManager.getCurrentSessionUserId(), "syllabus_access_athz", ""); } } catch (PermissionException e) { // logger.info(this + ".getEntries() in PostemTool " + e); FacesContext.getCurrentInstance() .addMessage( null, MessageUtils.getMessage( FacesMessage.SEVERITY_ERROR, "error_permission", (new Object[] {e.toString()}), FacesContext.getCurrentInstance())); this.currentGradebook = null; this.csv = null; this.newTemplate = null; // this.release = null; return "permission_error"; } if (currentGradebook.getId() == null) { ArrayList gb = getGradebooks(); Iterator gi = gb.iterator(); while (gi.hasNext()) { if (((Gradebook) gi.next()).getTitle().equals(currentGradebook.getTitle())) { // To stay consistent, remove current messages when adding a new message // so as to only display one error message before returning PostemTool.clearMessages(); PostemTool.populateMessage( FacesMessage.SEVERITY_ERROR, "duplicate_title", new Object[] {}); return "create_gradebook"; } } } if (currentGradebook.getTitle() == null || currentGradebook.getTitle().equals("")) { // To stay consistent, remove current messages when adding a new message // so as to only display one error message before returning PostemTool.clearMessages(); PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR, "missing_title", new Object[] {}); return "create_gradebook"; } else if (currentGradebook.getTitle().trim().length() > TITLE_MAX_LENGTH) { PostemTool.clearMessages(); PostemTool.populateMessage( FacesMessage.SEVERITY_ERROR, "title_too_long", new Object[] { new Integer(currentGradebook.getTitle().trim().length()), new Integer(TITLE_MAX_LENGTH) }); return "create_gradebook"; } Reference attachment = getAttachmentReference(); if (attachment == null) { return "create_gradebook"; } if (!this.delimiter.equals(COMMA_DELIM_STR) && !this.delimiter.equals(TAB_DELIM_STR)) { PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR, "invalid_delim", new Object[] {}); return "create_gradebook"; } if (attachment != null) { // logger.info("*** Non-Empty CSV!"); try { char csv_delim = CSV.COMMA_DELIM; if (this.delimiter.equals(TAB_DELIM_STR)) { csv_delim = CSV.TAB_DELIM; } // Read the data ContentResource cr = contentHostingService.getResource(attachment.getId()); // Check the type if (ResourceProperties.TYPE_URL.equalsIgnoreCase(cr.getContentType())) { // Going to need to read from a stream String csvURL = new String(cr.getContent()); // Load the URL csv = URLConnectionReader.getText(csvURL); if (LOG.isDebugEnabled()) { LOG.debug(csv); } } else { csv = new String(cr.getContent()); if (LOG.isDebugEnabled()) { LOG.debug(csv); } } CSV grades = new CSV(csv, withHeader, csv_delim); if (withHeader == true) { if (grades.getHeaders() != null) { List headingList = grades.getHeaders(); for (int col = 0; col < headingList.size(); col++) { String heading = (String) headingList.get(col).toString().trim(); // Make sure there are no blank headings if (heading == null || heading.equals("")) { PostemTool.populateMessage( FacesMessage.SEVERITY_ERROR, "blank_headings", new Object[] {}); return "create_gradebook"; } // Make sure the headings don't exceed max limit if (heading.length() > HEADING_MAX_LENGTH) { PostemTool.populateMessage( FacesMessage.SEVERITY_ERROR, "heading_too_long", new Object[] {new Integer(HEADING_MAX_LENGTH)}); return "create_gradebook"; } } } } if (grades.getStudents() != null) { if (!usernamesValid(grades)) { return "create_gradebook"; } if (hasADuplicateUsername(grades)) { return "create_gradebook"; } } if (this.newTemplate != null && this.newTemplate.trim().length() > 0) { if (this.newTemplate.trim().length() > TEMPLATE_MAX_LENGTH) { PostemTool.populateMessage( FacesMessage.SEVERITY_ERROR, "template_too_long", new Object[] { new Integer(this.newTemplate.trim().length()), new Integer(TEMPLATE_MAX_LENGTH) }); return "create_gradebook"; } } if (withHeader == true) { if (grades.getHeaders() != null) { PostemTool.populateMessage(FacesMessage.SEVERITY_INFO, "has_headers", new Object[] {}); } } if (grades.getStudents() != null) { PostemTool.populateMessage( FacesMessage.SEVERITY_INFO, "has_students", new Object[] {new Integer(grades.getStudents().size())}); } if (withHeader == true) { currentGradebook.setHeadings(grades.getHeaders()); } List slist = grades.getStudents(); if (oldGradebook.getId() != null && !this.userPressedBack) { Set oldStudents = currentGradebook.getStudents(); oldGradebook.setStudents(oldStudents); } currentGradebook.setStudents(new TreeSet()); // gradebookManager.saveGradebook(currentGradebook); Iterator si = slist.iterator(); while (si.hasNext()) { List ss = (List) si.next(); String uname = ((String) ss.remove(0)).trim(); // logger.info("[POSTEM] processCreate -- adding student " + // uname); gradebookManager.createStudentGradesInGradebook(uname, ss, currentGradebook); if (currentGradebook.getStudents().size() == 1) { currentGradebook.setFirstUploadedUsername( uname); // otherwise, the verify screen shows first in ABC order } } } catch (DataFormatException exception) { /* * TODO: properly subclass exception in order to allow for localized * messages (add getRowNumber/setRowNumber). Set exception message to be * key in .properties file */ PostemTool.populateMessage( FacesMessage.SEVERITY_ERROR, exception.getMessage(), new Object[] {}); return "create_gradebook"; } catch (IdUnusedException e) { PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), new Object[] {}); return "create_gradebook"; } catch (TypeException e) { PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), new Object[] {}); return "create_gradebook"; } catch (PermissionException e) { PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), new Object[] {}); return "create_gradebook"; } catch (ServerOverloadException e) { PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), new Object[] {}); return "create_gradebook"; } catch (IOException e) { PostemTool.populateMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), new Object[] {}); return "create_gradebook"; } } else if (this.csv != null) { // logger.info("**** Non Null Empty CSV!"); PostemTool.populateMessage( FacesMessage.SEVERITY_ERROR, "has_students", new Object[] {new Integer(0)}); currentGradebook.setHeadings(new ArrayList()); if (oldGradebook.getId() != null) { Set oldStudents = currentGradebook.getStudents(); oldGradebook.setStudents(oldStudents); } currentGradebook.setStudents(new TreeSet()); } if (this.newTemplate != null && this.newTemplate.trim().length() > 0) { currentGradebook.setTemplate(gradebookManager.createTemplate(newTemplate.trim())); } else if (this.newTemplate != null) { // logger.info("*** Non Null Empty Template!"); currentGradebook.setTemplate(null); } /* * if("No".equals(this.release)) { currentGradebook.setReleased(new * Boolean(false)); //logger.info("Set to No, " + * currentGradebook.getReleased()); } else { * currentGradebook.setReleased(new Boolean(true)); //logger.info("Set to * Yes, " + currentGradebook.getReleased()); } */ // gradebookManager.saveGradebook(currentGradebook); // logger.info(currentGradebook.getId()); // currentGradebook = null; if ((this.csv != null && this.csv.trim().length() > 0) || (this.newTemplate != null && this.newTemplate.trim().length() > 0)) { this.csv = null; this.newTemplate = null; return "verify"; } Iterator oi = oldGradebook.getStudents().iterator(); while (oi.hasNext()) { gradebookManager.deleteStudentGrades((StudentGrades) oi.next()); } this.userId = SessionManager.getCurrentSessionUserId(); currentGradebook.setLastUpdated(new Timestamp(new Date().getTime())); currentGradebook.setLastUpdater(this.userId); gradebookManager.saveGradebook(currentGradebook); this.currentGradebook = null; this.oldGradebook = null; this.withHeader = true; // this.gradebooks = null; return "main"; }
/* * (non-Javadoc) * * @see org.sakaiproject.service.legacy.entity.ResourceService#merge(java.lang.String, * org.w3c.dom.Element, java.lang.String, java.lang.String, java.util.Map, java.util.HashMap, * java.util.Set) */ public String merge( String siteId, Element root, String archivePath, String fromSiteId, Map attachmentNames, Map userIdTrans, Set userListAllowImport) { // buffer for the results log StringBuilder results = new StringBuilder(); // populate SyllabusItem int syDataCount = 0; SyllabusItem syItem = null; if (siteId != null && siteId.trim().length() > 0) { try { NodeList allChildrenNodes = root.getChildNodes(); int length = allChildrenNodes.getLength(); for (int i = 0; i < length; i++) { Node siteNode = allChildrenNodes.item(i); if (siteNode.getNodeType() == Node.ELEMENT_NODE) { Element siteElement = (Element) siteNode; if (siteElement.getTagName().equals(SITE_ARCHIVE)) { // sakai2 NodeList pageNodes = siteElement.getChildNodes(); // int lengthPageNodes = pageNodes.getLength(); // for (int p = 0; p < lengthPageNodes; p++) // { // Node pageNode = pageNodes.item(p); // if (pageNode.getNodeType() == Node.ELEMENT_NODE) // { // Element pageElement = (Element) pageNode; // if (pageElement.getTagName().equals(PAGE_ARCHIVE)) // { // NodeList syllabusNodes = pageElement.getChildNodes(); NodeList syllabusNodes = siteElement.getChildNodes(); int lengthSyllabusNodes = syllabusNodes.getLength(); for (int sn = 0; sn < lengthSyllabusNodes; sn++) { Node syNode = syllabusNodes.item(sn); if (syNode.getNodeType() == Node.ELEMENT_NODE) { Element syElement = (Element) syNode; if (syElement.getTagName().equals(SYLLABUS)) { // create a page and all syllabus tool to the page // sakai2 String page = // addSyllabusToolToPage(siteId,pageElement // .getAttribute(PAGE_NAME)); // SyllabusItem syllabusItem = syllabusManager // .createSyllabusItem(UserDirectoryService // .getCurrentUser().getId(), page, syElement // .getAttribute(SYLLABUS_REDIRECT_URL)); String page = addSyllabusToolToPage(siteId, siteElement.getAttribute(SITE_NAME)); // sakai2 SyllabusItem syllabusItem = syllabusManager // .createSyllabusItem(UserDirectoryService // .getCurrentUser().getId(), page, syElement // .getAttribute(SYLLABUS_REDIRECT_URL)); // sakai2 add-- SyllabusItem syllabusItem = syllabusManager.getSyllabusItemByContextId(page); if (syllabusItem == null) { syllabusItem = syllabusManager.createSyllabusItem( UserDirectoryService.getCurrentUser().getId(), page, syElement.getAttribute(SYLLABUS_REDIRECT_URL)); } // added htripath: get imported redirecturl, even if syllabus item is existing. else { if (syElement.getAttribute(SYLLABUS_REDIRECT_URL) != null) { syllabusItem.setRedirectURL(syElement.getAttribute(SYLLABUS_REDIRECT_URL)); syllabusManager.saveSyllabusItem(syllabusItem); } } // NodeList allSyllabiNodes = syElement.getChildNodes(); int lengthSyllabi = allSyllabiNodes.getLength(); for (int j = 0; j < lengthSyllabi; j++) { Node child2 = allSyllabiNodes.item(j); if (child2.getNodeType() == Node.ELEMENT_NODE) { Element syDataElement = (Element) child2; if (syDataElement.getTagName().equals(SYLLABUS_DATA)) { List attachStringList = new ArrayList(); syDataCount = syDataCount + 1; SyllabusData syData = new SyllabusDataImpl(); syData.setView(syDataElement.getAttribute(SYLLABUS_DATA_VIEW)); syData.setTitle(syDataElement.getAttribute(SYLLABUS_DATA_TITLE)); syData.setStatus(syDataElement.getAttribute(SYLLABUS_DATA_STATUS)); syData.setEmailNotification( syDataElement.getAttribute(SYLLABUS_DATA_EMAIL_NOTIFICATION)); NodeList allAssetNodes = syDataElement.getChildNodes(); int lengthSyData = allAssetNodes.getLength(); for (int k = 0; k < lengthSyData; k++) { Node child3 = allAssetNodes.item(k); if (child3.getNodeType() == Node.ELEMENT_NODE) { Element assetEle = (Element) child3; if (assetEle.getTagName().equals(SYLLABUS_DATA_ASSET)) { String charset = trimToNull(assetEle.getAttribute("charset")); if (charset == null) charset = "UTF-8"; String body = trimToNull(assetEle.getAttribute("syllabus_body-html")); if (body != null) { try { byte[] decoded = Base64.decodeBase64(body.getBytes("UTF-8")); body = new String(decoded, charset); } catch (Exception e) { logger.warn("Decode Syllabus: " + e); } } if (body == null) body = ""; String ret; ret = trimToNull(body); syData.setAsset(ret); /*decode NodeList assetStringNodes = assetEle .getChildNodes(); int lengthAssetNodes = assetStringNodes .getLength(); for (int l = 0; l < lengthAssetNodes; l++) { Node child4 = assetStringNodes.item(l); if (child4.getNodeType() == Node.TEXT_NODE) { Text textNode = (Text) child4; syData.setAsset(textNode.getData()); } }*/ } if (assetEle.getTagName().equals(SYLLABUS_ATTACHMENT)) { Element attachElement = (Element) child3; String oldUrl = attachElement.getAttribute("relative-url"); if (oldUrl.startsWith("/content/attachment/")) { String newUrl = (String) attachmentNames.get(oldUrl); if (newUrl != null) { //// if (newUrl.startsWith("/attachment/")) //// newUrl = "/content".concat(newUrl); attachElement.setAttribute( "relative-url", Validator.escapeQuestionMark(newUrl)); attachStringList.add(Validator.escapeQuestionMark(newUrl)); } } else if (oldUrl.startsWith( "/content/group/" + fromSiteId + "/")) { String newUrl = "/content/group/" + siteId + oldUrl.substring(15 + fromSiteId.length()); attachElement.setAttribute( "relative-url", Validator.escapeQuestionMark(newUrl)); attachStringList.add(Validator.escapeQuestionMark(newUrl)); } } } } int initPosition = syllabusManager.findLargestSyllabusPosition(syllabusItem).intValue() + 1; syData = syllabusManager.createSyllabusDataObject( syData.getTitle(), (new Integer(initPosition)), syData.getAsset(), syData.getView(), syData.getStatus(), syData.getEmailNotification()); Set attachSet = new TreeSet(); for (int m = 0; m < attachStringList.size(); m++) { ContentResource cr = contentHostingService.getResource((String) attachStringList.get(m)); ResourceProperties rp = cr.getProperties(); // SyllabusAttachment tempAttach = // syllabusManager.createSyllabusAttachmentObject( // // (String)attachStringList.get(m),rp.getProperty(ResourceProperties.PROP_DISPLAY_NAME)); SyllabusAttachment tempAttach = syllabusManager.createSyllabusAttachmentObject( cr.getId(), rp.getProperty(ResourceProperties.PROP_DISPLAY_NAME)); tempAttach.setName( rp.getProperty(ResourceProperties.PROP_DISPLAY_NAME)); tempAttach.setSize( rp.getProperty(ResourceProperties.PROP_CONTENT_LENGTH)); tempAttach.setType( rp.getProperty(ResourceProperties.PROP_CONTENT_TYPE)); tempAttach.setUrl(cr.getUrl()); tempAttach.setAttachmentId(cr.getId()); attachSet.add(tempAttach); } syData.setAttachments(attachSet); syllabusManager.addSyllabusToSyllabusItem(syllabusItem, syData); } } } } } } } } } results.append("merging syllabus " + siteId + " (" + syDataCount + ") syllabus items.\n"); } catch (DOMException e) { logger.error(e.getMessage(), e); results.append("merging " + getLabel() + " failed during xml parsing.\n"); } catch (Exception e) { logger.error(e.getMessage(), e); results.append("merging " + getLabel() + " failed.\n"); } } return results.toString(); }
public Map<String, String> transferCopyEntitiesRefMigrator( String fromContext, String toContext, List<String> ids) { Map<String, String> transversalMap = new HashMap<String, String>(); try { logger.debug("transfer copy syllbus itmes by transferCopyEntitiesRefMigrator"); String fromPage = fromContext; SyllabusItem fromSyllabusItem = syllabusManager.getSyllabusItemByContextId(fromPage); if (fromSyllabusItem != null) { Set fromSyDataSet = syllabusManager.getSyllabiForSyllabusItem(fromSyllabusItem); if ((fromSyDataSet != null && fromSyDataSet.size() > 0) || fromSyllabusItem.getRedirectURL() != null) { String toPage = addSyllabusToolToPage(toContext, SiteService.getSite(toContext).getTitle()); SyllabusItem toSyItem = syllabusManager.getSyllabusItemByContextId(toPage); if (toSyItem == null) { toSyItem = syllabusManager.createSyllabusItem( UserDirectoryService.getCurrentUser().getId(), toPage, fromSyllabusItem.getRedirectURL()); } else if (fromSyllabusItem.getRedirectURL() != null) { toSyItem.setRedirectURL(fromSyllabusItem.getRedirectURL()); syllabusManager.saveSyllabusItem(toSyItem); } Iterator fromSetIter = fromSyDataSet.iterator(); while (fromSetIter.hasNext()) { SyllabusData toSyData = (SyllabusData) fromSetIter.next(); Integer positionNo = new Integer(syllabusManager.findLargestSyllabusPosition(toSyItem).intValue() + 1); SyllabusData newToSyData = syllabusManager.createSyllabusDataObject( toSyData.getTitle(), positionNo, toSyData.getAsset(), toSyData.getView(), toSyData.getStatus(), toSyData.getEmailNotification()); Set attachSet = syllabusManager.getSyllabusAttachmentsForSyllabusData(toSyData); Iterator attachIter = attachSet.iterator(); Set newAttachSet = new TreeSet(); while (attachIter.hasNext()) { SyllabusAttachment thisAttach = (SyllabusAttachment) attachIter.next(); ContentResource oldAttachment = contentHostingService.getResource(thisAttach.getAttachmentId()); ContentResource attachment = contentHostingService.addAttachmentResource( oldAttachment .getProperties() .getProperty(ResourceProperties.PROP_DISPLAY_NAME), toContext, ToolManager.getTool("sakai.syllabus").getTitle(), oldAttachment.getContentType(), oldAttachment.getContent(), oldAttachment.getProperties()); SyllabusAttachment thisSyllabusAttach = syllabusManager.createSyllabusAttachmentObject( attachment.getId(), attachment.getProperties().getProperty(ResourceProperties.PROP_DISPLAY_NAME)); newAttachSet.add(thisSyllabusAttach); } newToSyData.setAttachments(newAttachSet); syllabusManager.addSyllabusToSyllabusItem(toSyItem, newToSyData); } } else { logger.debug( "importResources: no data found for syllabusItem id" + fromSyllabusItem.getSurrogateKey().toString()); } logger.debug("importResources: End importing syllabus data"); } } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage(), e); } return transversalMap; }