diff --git a/citations/citations-tool/tool/src/java/org/sakaiproject/citation/tool/CitationHelperAction.java b/citations/citations-tool/tool/src/java/org/sakaiproject/citation/tool/CitationHelperAction.java index c4eb04a0910d..12ef1aa87f3a 100644 --- a/citations/citations-tool/tool/src/java/org/sakaiproject/citation/tool/CitationHelperAction.java +++ b/citations/citations-tool/tool/src/java/org/sakaiproject/citation/tool/CitationHelperAction.java @@ -2380,277 +2380,279 @@ private String buildEditAttachmentPanelContext(VelocityPortlet portlet, Context return TEMPLATE_EDIT; } public String buildNewResourcePanelContext(VelocityPortlet portlet, Context context, RunData rundata, SessionState state) { + String citationCollectionId = null; + try { + logger.debug("buildNewResourcePanelContext()"); - logger.debug("buildNewResourcePanelContext()"); + context.put("MIMETYPE_JSON", MIMETYPE_JSON); + context.put("REQUESTED_MIMETYPE", REQUESTED_MIMETYPE); - context.put("MIMETYPE_JSON", MIMETYPE_JSON); - context.put("REQUESTED_MIMETYPE", REQUESTED_MIMETYPE); - - context.put("xilator", new Validator()); - - context.put("availability_is_enabled", Boolean.TRUE); - context.put("GROUP_ACCESS", AccessMode.GROUPED); - context.put("INHERITED_ACCESS", AccessMode.INHERITED); - - Boolean resourceAdd = (Boolean) state.getAttribute(STATE_RESOURCES_ADD); - if(resourceAdd != null && resourceAdd.equals(true)) { - context.put("resourceAdd", Boolean.TRUE); - context.put(CITATION_ACTION, CREATE_RESOURCE); - } else { - context.put(CITATION_ACTION, UPDATE_RESOURCE); - } - - // resource-related - String resourceId = (String) state.getAttribute(CitationHelper.RESOURCE_ID); - String resourceUuid = (String) state.getAttribute(CitationHelper.RESOURCE_UUID); - - if(resourceId == null || resourceId.trim().equals("")) { - if(resourceUuid == null || resourceUuid.trim().equals("")) { - // Will be dealt with later by creating new resource when needed - } else if(resourceUuid.startsWith("/")) { - // UUID and ID may be switched - resourceId = resourceUuid; - resourceUuid = this.getContentService().getUuid(resourceId); - if(resourceUuid != null) { - state.setAttribute(CitationHelper.RESOURCE_ID, resourceId); - state.setAttribute(CitationHelper.RESOURCE_UUID, resourceUuid); - } - } else { - // see if we can get the resourceId from the UUID - resourceId = this.getContentService().resolveUuid(resourceUuid); - if(resourceId != null) { - state.setAttribute(CitationHelper.RESOURCE_ID, resourceId); - } - } - } else if(resourceUuid == null || resourceUuid.trim().equals("")) { - resourceUuid = this.getContentService().getUuid(resourceId); - if(resourceUuid != null) { - state.setAttribute(CitationHelper.RESOURCE_UUID, resourceUuid); - } - } - - if(logger.isDebugEnabled()) { - logger.debug("buildNewResourcePanelContext() resourceUuid == " + resourceUuid + " resourceId == " + resourceId); - } - - String citationCollectionId = null; - ContentResource resource = null; - Map contentProperties = null; - if(resourceId == null) { - - } else { - try { - resource = getContentService().getResource(resourceId); - } catch (IdUnusedException e) { - logger.warn("IdUnusedException geting resource in buildNewResourcePanelContext() " + e); - } catch (TypeException e) { - logger.warn("TypeException geting resource in buildNewResourcePanelContext() " + e); - } catch (PermissionException e) { - logger.warn("PermissionException geting resource in buildNewResourcePanelContext() " + e); + context.put("xilator", new Validator()); + + context.put("availability_is_enabled", Boolean.TRUE); + context.put("GROUP_ACCESS", AccessMode.GROUPED); + context.put("INHERITED_ACCESS", AccessMode.INHERITED); + + Boolean resourceAdd = (Boolean) state.getAttribute(STATE_RESOURCES_ADD); + if (resourceAdd != null && resourceAdd.equals(true)) { + context.put("resourceAdd", Boolean.TRUE); + context.put(CITATION_ACTION, CREATE_RESOURCE); + } else { + context.put(CITATION_ACTION, UPDATE_RESOURCE); + } + + // resource-related + String resourceId = (String) state.getAttribute(CitationHelper.RESOURCE_ID); + String resourceUuid = (String) state.getAttribute(CitationHelper.RESOURCE_UUID); + + if (resourceId == null || resourceId.trim().equals("")) { + if (resourceUuid == null || resourceUuid.trim().equals("")) { + // Will be dealt with later by creating new resource when needed + } else if (resourceUuid.startsWith("/")) { + // UUID and ID may be switched + resourceId = resourceUuid; + resourceUuid = this.getContentService().getUuid(resourceId); + if (resourceUuid != null) { + state.setAttribute(CitationHelper.RESOURCE_ID, resourceId); + state.setAttribute(CitationHelper.RESOURCE_UUID, resourceUuid); + } + } else { + // see if we can get the resourceId from the UUID + resourceId = this.getContentService().resolveUuid(resourceUuid); + if (resourceId != null) { + state.setAttribute(CitationHelper.RESOURCE_ID, resourceId); + } + } + } else if (resourceUuid == null || resourceUuid.trim().equals("")) { + resourceUuid = this.getContentService().getUuid(resourceId); + if (resourceUuid != null) { + state.setAttribute(CitationHelper.RESOURCE_UUID, resourceUuid); + } } - -// String guid = getContentService().getUuid(resourceId); -// context.put("RESOURCE_ID", guid); - } - if(resource == null) { - context.put(CITATION_ACTION, CREATE_RESOURCE); - - ToolSession toolSession = getSessionManager().getCurrentToolSession(); - ResourceToolActionPipe pipe = (ResourceToolActionPipe) toolSession.getAttribute(ResourceToolAction.ACTION_PIPE); - String contentCollectionId = pipe.getContentEntity().getId(); - context.put("contentCollectionId", contentCollectionId); - ContentCollection collection; - try { - collection = getContentService().getCollection(contentCollectionId); - contentProperties = this.getProperties(collection, state); - } catch (IdUnusedException e) { - logger.warn("IdUnusedException geting collection in buildNewResourcePanelContext() " + e); - } catch (TypeException e) { - logger.warn("TypeException geting collection in buildNewResourcePanelContext() " + e); - } catch (PermissionException e) { - logger.warn("PermissionException geting collection in buildNewResourcePanelContext() " + e); + if (logger.isDebugEnabled()) { + logger.debug("buildNewResourcePanelContext() resourceUuid == " + resourceUuid + " resourceId == " + resourceId); } - } else { - ResourceProperties props = resource.getProperties(); - contentProperties = this.getProperties(resource, state); - context.put("resourceTitle", props.getProperty(ResourceProperties.PROP_DISPLAY_NAME)); - context.put("resourceDescription", props.getProperty(ResourceProperties.PROP_DESCRIPTION)); - context.put("resourceIntroduction", (props.getProperty(CitationService.PROP_INTRODUCTION) == null ? props.getProperty(ResourceProperties.PROP_DESCRIPTION) : props.getProperty(CitationService.PROP_INTRODUCTION))); - context.put("officialInstBackColour", scs.getString("official.institution.background.colour")); - context.put("officialInstTextColour", scs.getString("official.institution.text.colour")); - //resourceUuid = this.getContentService().getUuid(resourceId); - context.put("resourceUuid", resourceUuid ); - context.put("contentCollectionId", resource.getContainingCollection().getId()); - try { - citationCollectionId = new String(resource.getContent()); - if(citationCollectionId != null) { - state.setAttribute(STATE_CITATION_COLLECTION_ID, citationCollectionId); + + ContentResource resource = null; + Map contentProperties = null; + if (resourceId == null) { + + } else { + try { + resource = getContentService().getResource(resourceId); + } catch (IdUnusedException e) { + logger.warn("IdUnusedException geting resource in buildNewResourcePanelContext() " + e); + } catch (TypeException e) { + logger.warn("TypeException geting resource in buildNewResourcePanelContext() " + e); + } catch (PermissionException e) { + logger.warn("PermissionException geting resource in buildNewResourcePanelContext() " + e); } - } catch (ServerOverloadException e) { - logger.warn("ServerOverloadException geting props in buildNewResourcePanelContext() " + e); + +// String guid = getContentService().getUuid(resourceId); +// context.put("RESOURCE_ID", guid); } - context.put(CITATION_ACTION, UPDATE_RESOURCE); - } - if(contentProperties == null) { - contentProperties = new HashMap(); - } - context.put("contentProperties", contentProperties); - int collectionSize = 0; - CitationCollection citationCollection = getCitationCollection(state, true); - // nested sections and nested citations - CitationCollectionOrder nestedCollection = getCitationService().getNestedCollection(citationCollection.getId()); - context.put("nestedCollection", nestedCollection); - context.put("nestedSectionsSize", nestedCollection.getChildren().size()); + if (resource == null) { + context.put(CITATION_ACTION, CREATE_RESOURCE); - // unnested citations - CitationCollection unnestedCitationCollection = getCitationService().getUnnestedCitationCollection(citationCollection.getId()); - context.put("unnestedCitationCollection", unnestedCitationCollection); - context.put("unnestedCitationCollectionSize", unnestedCitationCollection.size()); + ToolSession toolSession = getSessionManager().getCurrentToolSession(); + ResourceToolActionPipe pipe = (ResourceToolActionPipe) toolSession.getAttribute(ResourceToolAction.ACTION_PIPE); + String contentCollectionId = pipe.getContentEntity().getId(); + context.put("contentCollectionId", contentCollectionId); + ContentCollection collection; + try { + collection = getContentService().getCollection(contentCollectionId); + contentProperties = this.getProperties(collection, state); + } catch (IdUnusedException e) { + logger.warn("IdUnusedException geting collection in buildNewResourcePanelContext() " + e); + } catch (TypeException e) { + logger.warn("TypeException geting collection in buildNewResourcePanelContext() " + e); + } catch (PermissionException e) { + logger.warn("PermissionException geting collection in buildNewResourcePanelContext() " + e); + } + } else { + ResourceProperties props = resource.getProperties(); + contentProperties = this.getProperties(resource, state); + context.put("resourceTitle", props.getProperty(ResourceProperties.PROP_DISPLAY_NAME)); + context.put("resourceDescription", props.getProperty(ResourceProperties.PROP_DESCRIPTION)); + context.put("resourceIntroduction", (props.getProperty(CitationService.PROP_INTRODUCTION) == null ? props.getProperty(ResourceProperties.PROP_DESCRIPTION) : props.getProperty(CitationService.PROP_INTRODUCTION))); + context.put("officialInstBackColour", scs.getString("official.institution.background.colour")); + context.put("officialInstTextColour", scs.getString("official.institution.text.colour")); + //resourceUuid = this.getContentService().getUuid(resourceId); + context.put("resourceUuid", resourceUuid); + context.put("contentCollectionId", resource.getContainingCollection().getId()); + try { + citationCollectionId = new String(resource.getContent()); + if (citationCollectionId != null) { + state.setAttribute(STATE_CITATION_COLLECTION_ID, citationCollectionId); + } + } catch (ServerOverloadException e) { + logger.warn("ServerOverloadException geting props in buildNewResourcePanelContext() " + e); + } + context.put(CITATION_ACTION, UPDATE_RESOURCE); + } + if (contentProperties == null) { + contentProperties = new HashMap(); + } + context.put("contentProperties", contentProperties); + int collectionSize = 0; + CitationCollection citationCollection = getCitationCollection(state, true); - // all citations - context.put("allCitationCollection", citationCollection); + // nested sections and nested citations + CitationCollectionOrder nestedCollection = getCitationService().getNestedCollection(citationCollection.getId()); + context.put("nestedCollection", nestedCollection); + context.put("nestedSectionsSize", nestedCollection.getChildren().size()); - if(citationCollection == null) { - logger.warn( "buildAddCitationsPanelContext unable to access citationCollection " + citationCollectionId ); + // unnested citations + CitationCollection unnestedCitationCollection = getCitationService().getUnnestedCitationCollection(citationCollection.getId()); + context.put("unnestedCitationCollection", unnestedCitationCollection); + context.put("unnestedCitationCollectionSize", unnestedCitationCollection.size()); - int requestStateId = preserveRequestState(state, new String[]{CitationHelper.RESOURCES_REQUEST_PREFIX, CitationHelper.CITATION_PREFIX}); - context.put("requestStateId", requestStateId); + // all citations + context.put("allCitationCollection", citationCollection); - return TEMPLATE_ERROR; - } else { - // get the size of the list - collectionSize = citationCollection.size(); - citationCollectionId = citationCollection.getId(); - } - context.put("citationCollectionId", citationCollectionId); - context.put( "collectionSize", new Integer( collectionSize ) ); - context.put( "sectionBackgroundColour", scs.getString("official.institution.background.colour")); - context.put( "sectionTextColour", scs.getString("official.institution.text.colour")); + if (citationCollection == null) { + logger.warn("buildAddCitationsPanelContext unable to access citationCollection " + citationCollectionId); + int requestStateId = preserveRequestState(state, new String[]{CitationHelper.RESOURCES_REQUEST_PREFIX, CitationHelper.CITATION_PREFIX}); + context.put("requestStateId", requestStateId); + + return TEMPLATE_ERROR; + } else { + // get the size of the list + collectionSize = citationCollection.size(); + citationCollectionId = citationCollection.getId(); + } + context.put("citationCollectionId", citationCollectionId); + context.put("collectionSize", new Integer(collectionSize)); + context.put("sectionBackgroundColour", scs.getString("official.institution.background.colour")); + context.put("sectionTextColour", scs.getString("official.institution.text.colour")); + + + Locale locale = rb.getLocale(); + List> saveciteClients = getConfigurationService().getSaveciteClientsForLocale(locale); + + if (saveciteClients != null) { + if (resource != null && resourceId != null) { + for (Map client : saveciteClients) { + String saveciteUrl = getSearchManager().getSaveciteUrl(resourceUuid, client.get("id")); + try { + client.put("saveciteUrl", java.net.URLEncoder.encode(saveciteUrl, "UTF-8")); + } catch (UnsupportedEncodingException e) { + logger.warn("Error encoding savecite URL", e); + } - Locale locale = rb.getLocale(); - List> saveciteClients = getConfigurationService().getSaveciteClientsForLocale(locale); - - if(saveciteClients != null) { - if(resource != null && resourceId != null) { - for(Map client : saveciteClients) { - String saveciteUrl = getSearchManager().getSaveciteUrl(resourceUuid,client.get("id")); - try { - client.put("saveciteUrl", java.net.URLEncoder.encode(saveciteUrl,"UTF-8")); - } catch (UnsupportedEncodingException e) { - logger.warn("Error encoding savecite URL",e); } - } + + context.put("saveciteClients", saveciteClients); } - - context.put("saveciteClients",saveciteClients); - } - // determine which features to display - if( getConfigurationService().isGoogleScholarEnabled() ) { - String googleUrl = getSearchManager().getGoogleScholarUrl(getContentService().getUuid(resourceId)); - String sakaiInstance = scs.getString("ui.service", "Sakai"); - context.put( "googleUrl", googleUrl ); + // determine which features to display + if (getConfigurationService().isGoogleScholarEnabled()) { + String googleUrl = getSearchManager().getGoogleScholarUrl(getContentService().getUuid(resourceId)); + String sakaiInstance = scs.getString("ui.service", "Sakai"); + context.put("googleUrl", googleUrl); - // object array for formatted messages - Object[] googleArgs = { rb.getFormattedMessage( "linkLabel.google.sakai", sakaiInstance ) }; - context.put( "googleArgs", googleArgs ); - } + // object array for formatted messages + Object[] googleArgs = {rb.getFormattedMessage("linkLabel.google.sakai", sakaiInstance)}; + context.put("googleArgs", googleArgs); + } - if( getConfigurationService().librarySearchEnabled() ) { - context.put( "searchLibrary", Boolean.TRUE ); - } - - if(citationCollection == null || (citationCollection.size() <= 0 && unnestedCitationCollection.size()<=0) && nestedCollection.getChildren().size()<=0) { - - } else { - context.put("openUrlLabel", getConfigurationService().getSiteConfigOpenUrlLabel()); - - String currentSort = (String) state.getAttribute(STATE_SORT); + if (getConfigurationService().librarySearchEnabled()) { + context.put("searchLibrary", Boolean.TRUE); + } - if (currentSort == null || currentSort.trim().length() == 0) - currentSort = citationCollection.getSort(); + if (citationCollection == null || (citationCollection.size() <= 0 && unnestedCitationCollection.size() <= 0) && nestedCollection.getChildren().size() <= 0) { - if(currentSort == null || currentSort.trim().length() == 0) { - currentSort = CitationCollection.SORT_BY_TITLE; - } - - context.put("currentSort", currentSort); - - String savedSort = citationCollection.getSort(); - if(savedSort == null || savedSort.trim().equals("")) { - savedSort = CitationCollection.SORT_BY_TITLE; - } - - if(savedSort != currentSort) { - - citationCollection.setSort(currentSort, true); - } - - //context.put(PARAM_FORM_NAME, ELEMENT_ID_LIST_FORM); + } else { + context.put("openUrlLabel", getConfigurationService().getSiteConfigOpenUrlLabel()); - // collection size - context.put( "collectionSize", new Integer( citationCollection.size() ) ); + String currentSort = (String) state.getAttribute(STATE_SORT); - // export URLs - String exportParams = "?resourceDisplayName=" + resource.getProperties().getProperty(ResourceProperties.PROP_DISPLAY_NAME) + "&resourceId=" + resourceId; - String exportUrlSel = citationCollection.getUrl(CitationService.REF_TYPE_EXPORT_RIS_SEL) + exportParams; - String exportUrlAll = citationCollection.getUrl(CitationService.REF_TYPE_EXPORT_RIS_ALL) + exportParams; - context.put("exportUrlSel", exportUrlSel); - context.put("exportUrlAll", exportUrlAll); + if (currentSort == null || currentSort.trim().length() == 0) + currentSort = citationCollection.getSort(); - Integer listPageSize = (Integer) state.getAttribute(STATE_LIST_PAGE_SIZE); - if(listPageSize == null) - { - listPageSize = defaultListPageSize; - state.setAttribute(STATE_LIST_PAGE_SIZE, listPageSize); - } - context.put("listPageSize", listPageSize); + if (currentSort == null || currentSort.trim().length() == 0) { + currentSort = CitationCollection.SORT_BY_TITLE; + } - CitationIterator newIterator = citationCollection.iterator(); - CitationIterator oldIterator = (CitationIterator) state.getAttribute(STATE_LIST_ITERATOR); - if(oldIterator == null) { - newIterator.setPageSize(listPageSize.intValue()); - newIterator.setStart(0); - } else { - newIterator.setPageSize(listPageSize.intValue()); - newIterator.setStart(oldIterator.getStart()); + context.put("currentSort", currentSort); + + String savedSort = citationCollection.getSort(); + if (savedSort == null || savedSort.trim().equals("")) { + savedSort = CitationCollection.SORT_BY_TITLE; + } + + if (savedSort != currentSort) { + + citationCollection.setSort(currentSort, true); + } + + //context.put(PARAM_FORM_NAME, ELEMENT_ID_LIST_FORM); + + // collection size + context.put("collectionSize", new Integer(citationCollection.size())); + + // export URLs + String exportParams = "?resourceDisplayName=" + resource.getProperties().getProperty(ResourceProperties.PROP_DISPLAY_NAME) + "&resourceId=" + resourceId; + String exportUrlSel = citationCollection.getUrl(CitationService.REF_TYPE_EXPORT_RIS_SEL) + exportParams; + String exportUrlAll = citationCollection.getUrl(CitationService.REF_TYPE_EXPORT_RIS_ALL) + exportParams; + context.put("exportUrlSel", exportUrlSel); + context.put("exportUrlAll", exportUrlAll); + + Integer listPageSize = (Integer) state.getAttribute(STATE_LIST_PAGE_SIZE); + if (listPageSize == null) { + listPageSize = defaultListPageSize; + state.setAttribute(STATE_LIST_PAGE_SIZE, listPageSize); + } + context.put("listPageSize", listPageSize); + + CitationIterator newIterator = citationCollection.iterator(); + CitationIterator oldIterator = (CitationIterator) state.getAttribute(STATE_LIST_ITERATOR); + if (oldIterator == null) { + newIterator.setPageSize(listPageSize.intValue()); + newIterator.setStart(0); + } else { + newIterator.setPageSize(listPageSize.intValue()); + newIterator.setStart(oldIterator.getStart()); // newIterator.setPage(oldIterator.getPage()); - } - context.put("citations", newIterator); - context.put("citationCollectionId", citationCollection.getId()); - if(! citationCollection.isEmpty()) - { - context.put("show_citations", Boolean.TRUE); + } + context.put("citations", newIterator); + context.put("citationCollectionId", citationCollection.getId()); + if (!citationCollection.isEmpty()) { + context.put("show_citations", Boolean.TRUE); // int page = newIterator.getPage(); // int pageSize = newIterator.getPageSize(); - int totalSize = citationCollection.size(); + int totalSize = citationCollection.size(); - int start = newIterator.getStart(); - int end = newIterator.getEnd(); + int start = newIterator.getStart(); + int end = newIterator.getEnd(); // int start = page * pageSize + 1; // int end = Math.min((page + 1) * pageSize, totalSize); - Integer[] position = { new Integer(start+1) , new Integer(end), new Integer(totalSize)}; - String showing = (String) rb.getFormattedMessage("showing.results", position); - context.put("showing", showing); - } - state.setAttribute(STATE_LIST_ITERATOR, newIterator); - - // constant schema identifier - context.put( "titleProperty", Schema.TITLE ); + Integer[] position = {new Integer(start + 1), new Integer(end), new Integer(totalSize)}; + String showing = (String) rb.getFormattedMessage("showing.results", position); + context.put("showing", showing); + } + state.setAttribute(STATE_LIST_ITERATOR, newIterator); - int requestStateId = preserveRequestState(state, new String[]{CitationHelper.RESOURCES_REQUEST_PREFIX, CitationHelper.CITATION_PREFIX}); - context.put("requestStateId", requestStateId); + // constant schema identifier + context.put("titleProperty", Schema.TITLE); - + int requestStateId = preserveRequestState(state, new String[]{CitationHelper.RESOURCES_REQUEST_PREFIX, CitationHelper.CITATION_PREFIX}); + context.put("requestStateId", requestStateId); + + + } + + return TEMPLATE_NEW_RESOURCE; + } + catch (RuntimeException e){ + throw new RuntimeException("Citation collection ID: "+ citationCollectionId, e); } - - return TEMPLATE_NEW_RESOURCE; } /**