def __isLinked(self, ids, map):
     query = 'package_node_id:("' + '" OR "'.join(ids) + '")'
     req = SearchRequest(query)
     req.setParam("fq", 'recordtype:"master"')
     req.addParam("fq", 'item_type:"object"')
     req.setParam("rows", "9999")
     
     out = ByteArrayOutputStream()
     self.__indexer.search(req, out)
     result = JsonConfigHelper(ByteArrayInputStream(out.toByteArray()))
     
     currentList = []
     for doc in result.getJsonList("response/docs"):
         currentList.extend(doc.getList("package_node_id"))
     
     if type(map).__name__ == "LinkedHashMap":
         for author in map.keySet():
             authorDocs = map.get(author)
             for doc in authorDocs:
                 if doc.get("id") in currentList:
                     doc.set("linked", "true")
     else:
         for author in map.keys():
             authorList = map[author]
             for count in authorList:
                 doc = authorList[count]
                 if doc.get("id") in currentList:
                     doc.set("linked", "true")
예제 #2
0
class QueuesData:
    def __init__(self):
        pass

    def __activate__(self, context):
        self.request = context["request"]
        self.response = context["response"]
        self.formData = context["formData"]

        if self.request.isXHR():
            print " **** formData: %s" % self.formData
            queue = self.formData.get("queueName")
            msg = self.formData.get("queueMessage")
            self.queueMessage(queue, msg);
            out = self.response.getPrintWriter("text/plain")
            out.println(self.formData)
            out.close()

        self.config = JsonConfigHelper(JsonConfig.getSystemFile())
        self.threads = self.config.getJsonList("messaging/threads")

    def getDescription(self, queue):
        for thread in self.threads:
            name = thread.get("config/name")
            if name == queue:
                return thread.get("description")

    def queueMessage(self, queue, msg):
        ms = MessagingServices.getInstance()
        ms.queueMessage(queue, msg);
        ms.release()
예제 #3
0
    def search_solr(self):
        query = "(rootUri:"
        if self.rootUriList:
            query += "(" + " OR ".join(self.rootUriList) + ")"
        else:
            query += "\"" + self.rootUri + "\""
        if self.type:
            query += " AND type:\"" + self.type + "\""
        query += ")"
        #print "**********", query

        req = SearchRequest(query)
        req.setParam("facet", "false")
        req.setParam("rows", str(99999))
        req.setParam("sort", "dateCreated asc")
        req.setParam("start", str(0))

        #security_roles = page.authentication.get_roles_list();
        #security_query = 'security_filter:("' + '" OR "'.join(security_roles) + '")'
        #req.addParam("fq", security_query)

        out = ByteArrayOutputStream()
        Services.indexer.annotateSearch(req, out)
        result = JsonConfigHelper(ByteArrayInputStream(out.toByteArray()))
        result = result.getJsonList("response/docs")

        # Every annotation for this URI
        if self.type == "http://www.purl.org/anotar/ns/type/0.1#Tag":
            return self.process_tags(result)
        else:
            return self.process_response(result)
    def getSuggestedNames(self):
        # search common forms
        lookupNames = []
        surname = self.__metadata.getList("surname").get(0)
        firstName = self.__metadata.getList("firstName").get(0)
        firstInitial = firstName[0].upper()
        secondName = self.__metadata.getList("secondName")
        if not secondName.isEmpty():
            secondName = secondName.get(0)
        if secondName and secondName != "":
            secondInitial = secondName[0].upper()
            lookupNames.append("%s, %s. %s." % (surname, firstInitial, secondInitial))
            lookupNames.append("%s, %s %s." % (surname, firstName, secondInitial))
            lookupNames.append("%s, %s %s" % (surname, firstName, secondName))
            lookupNames.append("%s %s %s" % (firstName, secondName, surname))
        lookupNames.append("%s, %s." % (surname, firstInitial))
        lookupNames.append("%s, %s" % (surname, firstName))
        lookupNames.append("%s %s" % (firstName, surname))
        query = '" OR dc_title:"'.join(lookupNames)

        # general word search from each part of the name
        parts = [p for p in self.getPackageTitle().split(" ") if len(p) > 0]
        query2 = " OR dc_title:".join(parts)

        req = SearchRequest('(dc_title:"%s")^2.5 OR (dc_title:%s)^0.5' % (query, query2))
        self.log.info("suggestedNames query={}", req.query)
        req.setParam("fq", 'recordtype:"author"')
        req.addParam("fq", 'item_type:"object"')
        req.setParam("rows", "9999")
        req.setParam("fl", "score")
        req.setParam("sort", "score desc")

        # Make sure 'fq' has already been set in the session
        ##security_roles = self.authentication.get_roles_list();
        ##security_query = 'security_filter:("' + '" OR "'.join(security_roles) + '")'
        ##req.addParam("fq", security_query)

        out = ByteArrayOutputStream()
        indexer = self.services.getIndexer()
        indexer.search(req, out)
        result = JsonConfigHelper(ByteArrayInputStream(out.toByteArray()))

        # self.log.info("result={}", result.toString())
        docs = result.getJsonList("response/docs")

        map = LinkedHashMap()
        for doc in docs:
            authorName = doc.getList("dc_title").get(0)
            if map.containsKey(authorName):
                authorDocs = map.get(authorName)
            else:
                authorDocs = ArrayList()
                map.put(authorName, authorDocs)
            authorDocs.add(doc)

        self.__maxScore = max(1.0, float(result.get("response/maxScore")))

        return map
예제 #5
0
class Queues:
    def __init__(self):
        self.config = JsonConfigHelper(JsonConfig.getSystemFile())
        self.threads = self.config.getJsonList("messaging/threads")

    def getDescription(self, queue):
        for thread in self.threads:
            name = thread.get("config/name")
            if name == queue:
                return thread.get("description")
 def __getMetadata(self, oid):
     req = SearchRequest('id:%s' % oid)
     req.setParam("fq", 'item_type:"object"')
     
     # Make sure 'fq' has already been set in the session
     ##security_roles = self.authentication.get_roles_list();
     ##security_query = 'security_filter:("' + '" OR "'.join(security_roles) + '")'
     ##req.addParam("fq", security_query)
     
     out = ByteArrayOutputStream()
     self.__indexer.search(req, out)
     result = JsonConfigHelper(ByteArrayInputStream(out.toByteArray()))
     #self.log.info("result={}", result.toString())
     return result.getJsonList("response/docs").get(0)
 def __getAuthorDetails(self, citationIds):
     query = " OR id:".join(citationIds)
     req = SearchRequest('id:%s' % query)
     req.setParam("fq", 'recordtype:"author"')
     req.addParam("fq", 'item_type:"object"')
     req.setParam("rows", "9999")
     
     # Make sure 'fq' has already been set in the session
     ##security_roles = self.authentication.get_roles_list();
     ##security_query = 'security_filter:("' + '" OR "'.join(security_roles) + '")'
     ##req.addParam("fq", security_query)
     
     out = ByteArrayOutputStream()
     self.__indexer.search(req, out)
     result = JsonConfigHelper(ByteArrayInputStream(out.toByteArray()))
     return result.getJsonList("response/docs")
예제 #8
0
 def __init__(self):
     responseType = "text/html; charset=UTF-8"
     responseMsg = ""
     func = formData.get("func")
     if func == "placeName":
         try:
             placeName = formData.get("q")
             url = "http://ws.geonames.org/searchJSON?fuzzy=0.7&q=" + placeName
             client = BasicHttpClient(url)
             get = GetMethod(url)
             statusInt = client.executeMethod(get)
             r = str(statusInt)
             jsonConfigHelper = JsonConfigHelper(get.getResponseBodyAsString().strip())
             for geoName in jsonConfigHelper.getJsonList("geonames"):
                 responseMsg += "%s, %s|%s \n" % (geoName.get("name"), geoName.get("countryName"), geoName.get("geonameId"))
         except Exception, e:
             print "exception: ", str(e)
             r = str(e), None
         responseType = "text/plain; charset=UTF-8"
예제 #9
0
 def getAuthorities(self):
     req = SearchRequest('package_node_id:%s' % self.metadata.get("id"))
     req.setParam("fq", 'recordtype:"master"')
     req.addParam("fq", 'item_type:"object"')
     req.setParam("rows", "9999")
     
     # Make sure 'fq' has already been set in the session
     ##security_roles = self.authentication.get_roles_list();
     ##security_query = 'security_filter:("' + '" OR "'.join(security_roles) + '")'
     ##req.addParam("fq", security_query)
     
     out = ByteArrayOutputStream()
     indexer = self.services.getIndexer()
     indexer.search(req, out)
     result = JsonConfigHelper(ByteArrayInputStream(out.toByteArray()))
     
     docs = result.getJsonList("response/docs")
     
     return docs
 def __searchNames(self, searchText):
     # search common forms
     lookupNames = []
     
     req = SearchRequest('(dc_title:"%s")^2.5' % searchText)
     self.log.info("searchNames query={}", req.query)
     req.setParam("fq", 'recordtype:"author"')
     req.addParam("fq", 'item_type:"object"')
     req.setParam("rows", "9999")
     req.setParam("fl", "score")
     req.setParam("sort", "score desc")
     
     out = ByteArrayOutputStream()
     self.__indexer.search(req, out)
     result = JsonConfigHelper(ByteArrayInputStream(out.toByteArray()))
     
     docs = result.getJsonList("response/docs")
     
     #Using this map because velocity gave error if LinkHashedMap is used
     map={}
     idList = []
     count = 0
     for doc in docs:
         authorName = str(doc.getList("dc_title").get(0))
         idList.append(doc.get("id"))
         if map.has_key(authorName):
             docsDic = map.get(authorName)
         else:
             docsDic = {}
             map[authorName] = docsDic
         #hash storageId and authorName
         doc.set("authorHash", self.getHash(authorName))
         doc.set("storageHash", self.getHash(doc.get("storage_id")))
         doc.set("affiliation", self.getCitationAffiliation(doc))
         ##doc.set("linked", Boolean.toString(linked))
         docsDic["%s" % count] = doc
         count +=1
     
     if idList:
         self.__isLinked(idList, map)
     return map
예제 #11
0
    def process_tags(self, result):
        tags = []
        tagsDict = {}
        # Build a dictionary of the tags
        for doc in result:
            doc = JsonConfigHelper(doc.get("jsonString"))
            tag = doc.get("content/literal")
            locs = doc.getJsonList("annotates/locators").size()
            if locs == 0:
                if tag in tagsDict:
                    d = tagsDict[tag]
                    d.set("tagCount", str(int(d.get("tagCount")) + 1))
                else:
                    doc.set("tagCount", str(1))
                    tagsDict[tag] = doc
            else:
                tags.append(doc.toString())

        for tag in tagsDict:
            tags.append(tagsDict[tag].toString())

        return "[" + ",".join(tags) + "]"
예제 #12
0
 def getSearchTerms(self):
     searchTerms = []
     
     prefix = self.getSuggestionPrefix()
     query = '%(prefix)s OR %(prefix)s*' % { "prefix" : prefix }
     req = SearchRequest(query)
     req.addParam("fq", self.page.getPortal().getQuery())
     req.addParam("fq", 'item_type:"object"')
     req.setParam("rows", "50")
     req.setParam("fl", "score,id,dc_title")
     req.setParam("sort", "score desc")
     
     out = ByteArrayOutputStream()
     indexer = self.services.getIndexer()
     indexer.search(req, out)
     result = JsonConfigHelper(ByteArrayInputStream(out.toByteArray()))
     
     docs = result.getJsonList("response/docs")
     for doc in docs:
         dc_title = doc.getList("dc_title").get(0)
         searchTerms.append(dc_title)
     
     return '", "'.join(searchTerms)
예제 #13
0
    def __getSolrData(self):
        prefix = self.getSearchTerms()
        print "prefix='%s'" % prefix
        if prefix:
            query = "dc_title:%(prefix)s OR dc_title:%(prefix)s*" % {"prefix": prefix}
            query += " OR f_dc_identifier:%(ns)s%(prefix)s OR f_dc_identifier:%(ns)s%(prefix)s*" % {
                "prefix": prefix,
                "ns": "http\://example.com/arc/",
            }
        else:
            query = "*:*"

        portal = self.services.portalManager.get(self.portalId)
        if portal.searchQuery != "*:*" and portal.searchQuery != "":
            query = query + " AND " + portal.searchQuery
        req = SearchRequest(query)
        req.setParam("fq", 'item_type:"object"')
        if portal.query:
            req.addParam("fq", portal.query)
        req.setParam("fl", "score")
        req.setParam("sort", "score desc")
        req.setParam("start", self.getStartIndex())
        req.setParam("rows", self.getItemsPerPage())
        req.setParam("facet", "true")
        req.setParam("facet.field", "repository_name")
        req.setParam("facet.mincount", "1")

        ns = self.getNamespace()
        level = self.getFormData("level", None)
        if level and level != "top":
            req.addParam("fq", 'repository_name:"%s"' % level.replace(ns, ""))

        try:
            out = ByteArrayOutputStream()
            indexer = self.services.getIndexer()
            indexer.search(req, out)
            results = JsonConfigHelper(ByteArrayInputStream(out.toByteArray()))
            if level == "top":
                narrowerMap = {}
                for doc in results.getJsonList("response/docs"):
                    value = doc.getList("repository_name").get(0)
                    hash = md5.md5(value).hexdigest()
                    if not narrowerMap.has_key(hash):
                        # print value, hash
                        narrowerMap[hash] = []
                    narrowerMap[hash].append(doc.get("id"))
                docs = ArrayList()
                facets = results.getList("facet_counts/facet_fields/repository_name")
                for i in range(0, len(facets), 2):
                    value = facets[i]
                    hash = md5.md5(value).hexdigest()
                    # print value,hash
                    doc = JsonConfigHelper()
                    doc.set("score", "1")
                    doc.set("dc_identifier", "%s%s" % (ns, value))
                    doc.set("skos_inScheme", ns)
                    doc.set("skos_broader", "%s%s" % (ns, value))
                    doc.set("skos_narrower", '", "'.join(narrowerMap[hash]))
                    doc.set("skos_prefLabel", value)
                    docs.add(doc)
                results.removePath("response/docs")
                results.setJsonList("response/docs", docs)
            return results
        except Exception, e:
            self.log.error("Failed to lookup '{}': {}", prefix, str(e))
 def getSuggestedNames(self):
     # search common forms
     lookupNames = []
     surname = self.__metadata.getList("surname").get(0)
     firstName = self.__metadata.getList("firstName").get(0)
     firstInitial = firstName[0].upper()
     secondName = self.__metadata.getList("secondName")
     if not secondName.isEmpty():
         secondName = secondName.get(0)
     if secondName and secondName != "":
         secondInitial = secondName[0].upper()
         lookupNames.append("%s, %s. %s." % (surname, firstInitial, secondInitial))
         lookupNames.append("%s, %s %s." % (surname, firstName, secondInitial))
         lookupNames.append("%s, %s %s" % (surname, firstName, secondName))
         lookupNames.append("%s %s %s" % (firstName, secondName, surname))
     lookupNames.append("%s, %s." % (surname, firstInitial))
     lookupNames.append("%s, %s" % (surname, firstName))
     lookupNames.append("%s %s" % (firstName, surname))
     query = '" OR dc_title:"'.join(lookupNames)
     
     # general word search from each part of the name
     parts = [p for p in self.getPackageTitle().split(" ") if len(p) > 0]
     query2 = " OR dc_title:".join(parts)
     
     #filter out the linked citation
     linkedCitations = self.__manifest.getList("//children//id")
     query3 = ""
     if linkedCitations:
         query3 = " OR ".join(linkedCitations)
         query3 = " AND -id:(%s)" % query3
     
     req = SearchRequest('(dc_title:"%s")^2.5 OR (dc_title:%s)^0.5%s' % (query, query2, query3))
     self.log.info("suggestedNames query={}", req.query)
     req.setParam("fq", 'recordtype:"author"')
     req.addParam("fq", 'item_type:"object"')
     req.setParam("rows", "9999")
     req.setParam("fl", "score")
     req.setParam("sort", "score desc")
     
     # Make sure 'fq' has already been set in the session
     ##security_roles = self.authentication.get_roles_list();
     ##security_query = 'security_filter:("' + '" OR "'.join(security_roles) + '")'
     ##req.addParam("fq", security_query)
     
     out = ByteArrayOutputStream()
     self.__indexer.search(req, out)
     result = JsonConfigHelper(ByteArrayInputStream(out.toByteArray()))
     
     #self.log.info("result={}", result.toString())
     docs = result.getJsonList("response/docs")
     
     exactMatchRecords = LinkedHashMap()
     map = LinkedHashMap()
     
     idList = []
     
     for doc in docs:
         authorName = doc.getList("dc_title").get(0)
         rank = self.getRank(doc.getList("score").get(0))
         id = doc.get("id")
         idList.append(id)
         #try to do automatch
         if float(rank) == 100.00 and self.isModified() == "false":
             if exactMatchRecords.containsKey(authorName):
                 authorMatchDocs = exactMatchRecords.get(authorName)
             else:
                 authorMatchDocs = ArrayList()
                 exactMatchRecords.put(authorName, authorMatchDocs)
             authorMatchDocs.add(doc)
         elif id not in linkedCitations:
             if map.containsKey(authorName):
                 authorDocs = map.get(authorName)
             else:
                 authorDocs = ArrayList()
                 map.put(authorName, authorDocs)
             authorDocs.add(doc)
     
     self.__maxScore = max(1.0, float(result.get("response/maxScore")))
     
     if idList:
         self.__isLinked(idList, map)
     
     # Do not auto save if record is live
     if self.__workflowMetadata.get("modified") == "false":
         self.__autoSaveExactRecord(exactMatchRecords)
     
     return map
예제 #15
0
class DetailData:
    def __init__(self):
        self.userAgreement = AgreementData(bindings)

    def __activate__(self, context):
        self.velocityContext = context
        self.services = context["Services"]
        self.request = context["request"]
        self.response = context["response"]
        self.contextPath = context["contextPath"]
        self.formData = context["formData"]
        self.page = context["page"]

        self.uaActivated = False
        useDownload = Boolean.parseBoolean(self.formData.get("download", "true"))
        self.__isPreview = Boolean.parseBoolean(self.formData.get("preview", "false"))
        self.__previewPid = None
        self.__hasPid = False

        uri = URLDecoder.decode(self.request.getAttribute("RequestURI"))
        matches = re.match("^(.*?)/(.*?)/(?:(.*?)/)?(.*)$", uri)
        if matches and matches.group(3):
            oid = matches.group(3)
            pid = matches.group(4)

            self.__metadata = JsonConfigHelper()
            self.__object = self.__getObject(oid)
            self.__oid = oid

            # If we have a PID
            if pid:
                self.__hasPid = True
                if useDownload:
                    # Download the payload to support relative links
                    download = DownloadData()
                    download.__activate__(context)
                else:
                    # Render the detail screen with the alternative preview
                    self.__readMetadata(oid)
                    self.__previewPid = pid
            # Otherwise, render the detail screen
            else:
                self.__readMetadata(oid)
                self.__previewPid = self.getPreview()

            if self.__previewPid:
                self.__previewPid = URLEncoder.encode(self.__previewPid, "UTF-8")
        else:
            # require trailing slash for relative paths
            q = ""
            if self.__isPreview:
                q = "?preview=true"
            self.response.sendRedirect("%s/%s/%s" % (self.contextPath, uri, q))

    def getAllowedRoles(self):
        metadata = self.getMetadata()
        if metadata is not None:
            return metadata.getList("security_filter")
        else:
            return []

    def getAllPreviews(self):
        list = self.getAltPreviews()
        preview = self.getPreview()
        if not list.contains(preview):
            list.add(preview)
        return list

    def getAltPreviews(self):
        return self.__metadata.getList("altpreview")

    def getFileName(self):
        return self.getObject().getSourceId()

    def getFileNameSplit(self, index):
        return os.path.splitext(self.getFileName())[index]

    def getFriendlyName(self, name):
        if name.startswith("dc_"):
            name = name[3:]
        if name.startswith("meta_"):
            name = name[5:]
        return name.replace("_", " ").capitalize()

    def getMetadata(self):
        return self.__metadata

    def getMetadataMap(self):
        return self.__metadataMap

    def getObject(self):
        return self.__object

    def getOid(self):
        return self.__oid

    def getPreview(self):
        return self.__metadata.get("preview")

    def getPreviewPid(self):
        return self.__previewPid

    def getProperty(self, field):
        return self.getObject().getMetadata().getProperty(field)

    def getUserAgreement(self):
        if not self.uaActivated:
            self.userAgreement.__activate__(self.velocityContext, self.getMetadata())
            self.uaActivated = True
        return self.userAgreement

    def hasLocalFile(self):
        # get original file.path from object properties
        filePath = self.getProperty("file.path")
        return filePath and os.path.exists(filePath)

    def hasPid(self):
        return self.__hasPid

    def isAccessDenied(self):
        myRoles = self.page.authentication.get_roles_list()
        allowedRoles = self.getAllowedRoles()
        for role in myRoles:
            if role in allowedRoles:
                return False
        return True

    def isDetail(self):
        return not (self.request.isXHR() or self.__isPreview)

    def isIndexed(self):
        return self.__getNumFound() == 1

    def isPending(self):
        meta = self.getObject().getMetadata()
        status = meta.get("render-pending")
        return Boolean.parseBoolean(status)

    def setStatus(self, status):
        self.response.setStatus(status)

    def __getNumFound(self):
        return int(self.__solrData.get("response/numFound"))

    def __getObject(self, oid):
        obj = None
        try:
            storage = self.services.getStorage()
            try:
                obj = storage.getObject(oid)
            except StorageException:
                sid = self.__getStorageId(oid)
                if sid is not None:
                    obj = storage.getObject(sid)
                    print "Object not found: oid='%s', trying sid='%s'" % (oid, sid)
        except StorageException:
            print "Object not found: oid='%s'" % oid
        return obj

    def __getStorageId(self, oid):
        return self.__metadata.get("storage_id")

    def __loadSolrData(self, oid):
        portal = self.page.getPortal()
        query = 'id:"%s"' % oid
        if self.isDetail() and portal.getSearchQuery():
            query += " AND " + portal.getSearchQuery()
        req = SearchRequest(query)
        req.addParam("fq", 'item_type:"object"')
        if self.isDetail():
            req.addParam("fq", portal.getQuery())
        out = ByteArrayOutputStream()
        self.services.getIndexer().search(req, out)
        self.__solrData = JsonConfigHelper(ByteArrayInputStream(out.toByteArray()))

    def __readMetadata(self, oid):
        self.__loadSolrData(oid)
        if self.isIndexed():
            self.__metadata = self.__solrData.getJsonList("response/docs").get(0)
            if self.__object is None:
                # Try again, indexed records might have a special storage_id
                self.__object = self.__getObject(oid)
            # Just a more usable instance of metadata
            self.__json = JsonConfigHelper(self.__solrData.getList("response/docs").get(0))
            self.__metadataMap = TreeMap(self.__json.getMap("/"))
        else:
            self.__metadata.set("id", oid)
예제 #16
0
    def __search(self):
        requireEscape = False
        recordsPerPage = self.__portal.recordsPerPage
        uri = URLDecoder.decode(self.request.getAttribute("RequestURI"))
        query = None
        pagePath = self.__portal.getName() + "/" + self.pageName
        if query is None or query == "":
            query = self.formData.get("query")
            requireEscape = True
        if query is None or query == "":
            query = "*:*"

        if query == "*:*":
            self.__query = ""
        else:
            self.__query = query
            if requireEscape:
                query = self.__escapeQuery(query)
            query = "%s:%s" % (self.__searchField, query)
        self.sessionState.set("query", self.__query)

        # find objects with annotations matching the query
        if query != "*:*":
            anotarQuery = self.__query
            if requireEscape:
                anotarQuery = self.__escapeQuery(anotarQuery)
            annoReq = SearchRequest(anotarQuery)
            annoReq.setParam("facet", "false")
            annoReq.setParam("rows", str(99999))
            annoReq.setParam("sort", "dateCreated asc")
            annoReq.setParam("start", str(0))
            anotarOut = ByteArrayOutputStream()
            self.services.indexer.annotateSearch(annoReq, anotarOut)
            resultForAnotar = JsonConfigHelper(ByteArrayInputStream(anotarOut.toByteArray()))
            resultForAnotar = resultForAnotar.getJsonList("response/docs")
            ids = HashSet()
            for annoDoc in resultForAnotar:
                annotatesUri = annoDoc.get("annotatesUri")
                ids.add(annotatesUri)
                print "Found annotation for %s" % annotatesUri
            # add annotation ids to query
            query += ' OR id:("' + '" OR "'.join(ids) + '")'

        portalSearchQuery = self.__portal.searchQuery
        if portalSearchQuery == "":
            portalSearchQuery = query
        else:
            if query != "*:*":
                query += " AND " + portalSearchQuery
            else:
                query = portalSearchQuery

        req = SearchRequest(query)
        req.setParam("facet", "true")
        req.setParam("rows", str(recordsPerPage))
        req.setParam("facet.field", self.__portal.facetFieldList)
        req.setParam("facet.sort", Boolean.toString(self.__portal.getFacetSort()))
        req.setParam("facet.limit", str(self.__portal.facetCount))
        req.setParam("sort", self.__sortBy)

        # setup facets
        if self.__useSessionNavigation:
            action = self.formData.get("verb")
            value = self.formData.get("value")
            fq = self.sessionState.get("fq")
            if fq is not None:
                self.__pageNum = 1
                req.setParam("fq", fq)
            if action == "add_fq":
                self.__pageNum = 1
                req.addParam("fq", URLDecoder.decode(value, "UTF-8"))
            elif action == "remove_fq":
                self.__pageNum = 1
                req.removeParam("fq", URLDecoder.decode(value, "UTF-8"))
            elif action == "clear_fq":
                self.__pageNum = 1
                req.removeParam("fq")
            elif action == "select-page":
                self.__pageNum = int(value)
        else:
            navUri = uri[len(pagePath) :]
            self.__pageNum, fq, self.__fqParts = self.__parseUri(navUri)
            savedfq = self.sessionState.get("savedfq")
            limits = []
            if savedfq:
                limits.extend(savedfq)
            if fq:
                limits.extend(fq)
                self.sessionState.set("savedfq", limits)
                for q in fq:
                    req.addParam("fq", URLDecoder.decode(q, "UTF-8"))

        portalQuery = self.__portal.query
        if portalQuery:
            req.addParam("fq", portalQuery)
        req.addParam("fq", 'item_type:"object"')
        if req.getParams("fq"):
            self.__selected = ArrayList(req.getParams("fq"))

        if self.__useSessionNavigation:
            self.sessionState.set("fq", self.__selected)
            self.sessionState.set("searchQuery", portalSearchQuery)
            self.sessionState.set("pageNum", self.__pageNum)

        # Make sure 'fq' has already been set in the session
        if not self.page.authentication.is_admin():
            current_user = self.page.authentication.get_username()
            security_roles = self.page.authentication.get_roles_list()
            security_filter = 'security_filter:("' + '" OR "'.join(security_roles) + '")'
            security_exceptions = 'security_exception:"' + current_user + '"'
            owner_query = 'owner:"' + current_user + '"'
            security_query = "(" + security_filter + ") OR (" + security_exceptions + ") OR (" + owner_query + ")"
            req.addParam("fq", security_query)

        req.setParam("start", str((self.__pageNum - 1) * recordsPerPage))

        print " * search.py:", req.toString(), self.__pageNum

        out = ByteArrayOutputStream()
        self.services.indexer.search(req, out)
        self.__result = JsonConfigHelper(ByteArrayInputStream(out.toByteArray()))
        if self.__result is not None:
            self.__paging = Pagination(
                self.__pageNum, int(self.__result.get("response/numFound")), self.__portal.recordsPerPage
            )
예제 #17
0
    def __search(self):
        recordsPerPage = self.__portal.recordsPerPage
        
        uri = URLDecoder.decode(request.getAttribute("RequestURI"))
        query = None
        pagePath = portalId + "/" + pageName
        if uri != pagePath:
            query = uri[len(pagePath)+1:]
        if query is None or query == "":
            query = formData.get("query")
        if query is None or query == "":
            query = "*:*"
        
        if query == "*:*":
            self.__query = ""
        else:
            self.__query = query
        sessionState.set("query", self.__query)
        
        # find objects with annotations matching the query
        if query != "*:*":
            anotarQuery = self.__query
            annoReq = SearchRequest(anotarQuery)
            annoReq.setParam("facet", "false")
            annoReq.setParam("rows", str(99999))
            annoReq.setParam("sort", "dateCreated asc")
            annoReq.setParam("start", str(0))        
            anotarOut = ByteArrayOutputStream()
            Services.indexer.annotateSearch(annoReq, anotarOut)
            resultForAnotar = JsonConfigHelper(ByteArrayInputStream(anotarOut.toByteArray()))
            resultForAnotar = resultForAnotar.getJsonList("response/docs")
            ids = HashSet()
            for annoDoc in resultForAnotar:
                annotatesUri = annoDoc.get("annotatesUri")
                ids.add(annotatesUri)
                print "Found annotation for %s" % annotatesUri
            # add annotation ids to query
            query += ' OR id:("' + '" OR "'.join(ids) + '")'

        portalSearchQuery = self.__portal.searchQuery
        if portalSearchQuery == "":
            portalSearchQuery = query
        else:
            if query != "*:*":
                query += " AND " + portalSearchQuery
            else:
                query = portalSearchQuery

        req = SearchRequest(query)
        req.setParam("facet", "true")
        req.setParam("rows", str(recordsPerPage))
        req.setParam("facet.field", self.__portal.facetFieldList)
        req.setParam("facet.sort", "true")
        req.setParam("facet.limit", str(self.__portal.facetCount))
        req.setParam("sort", "f_dc_title asc")
        
        # setup facets
        action = formData.get("verb")
        value = formData.get("value")
        fq = sessionState.get("fq")
        if fq is not None:
            self.__pageNum = 1
            req.setParam("fq", fq)
        if action == "add_fq":
            self.__pageNum = 1
            name = formData.get("name")
            print " * add_fq: %s" % value
            req.addParam("fq", URLDecoder.decode(value, "UTF-8"))
        elif action == "remove_fq":
            self.__pageNum = 1
            req.removeParam("fq", URLDecoder.decode(value, "UTF-8"))
        elif action == "clear_fq":
            self.__pageNum = 1
            req.removeParam("fq")
        elif action == "select-page":
            self.__pageNum = int(value)
        req.addParam("fq", 'item_type:"object"')
        
        portalQuery = self.__portal.query
        print " * portalQuery=%s" % portalQuery
        if portalQuery:
            req.addParam("fq", portalQuery)
        
        self.__selected = list(req.getParams("fq"))

        sessionState.set("fq", self.__selected)
        sessionState.set("searchQuery", portalSearchQuery)
        sessionState.set("pageNum", self.__pageNum)
        
        # Make sure 'fq' has already been set in the session
        if not page.authentication.is_admin():
            security_roles = page.authentication.get_roles_list()
            security_query = 'security_filter:("' + '" OR "'.join(security_roles) + '")'
            current_user = page.authentication.get_username()
            owner_query = 'owner:"' + current_user + '"'
            req.addParam("fq", "(" + security_query + ") OR (" + owner_query + ")")

        req.setParam("start", str((self.__pageNum - 1) * recordsPerPage))
        
        print " * search.py:", req.toString(), self.__pageNum
        
        out = ByteArrayOutputStream()
        Services.indexer.search(req, out)
        self.__result = JsonConfigHelper(ByteArrayInputStream(out.toByteArray()))
        if self.__result is not None:
            self.__paging = Pagination(self.__pageNum,
                                       int(self.__result.get("response/numFound")),
                                       self.__portal.recordsPerPage)
예제 #18
0
    def __workflow(self):
        # Workflow data
        WORKFLOW_ID = "packaging"
        wfChanged = False
        workflow_security = []
        self.message_list = None
        try:
            wfPayload = self.object.getPayload("workflow.metadata")
            wfMeta = JsonConfigHelper(wfPayload.open())
            wfPayload.close()

            # Are we indexing because of a workflow progression?
            targetStep = wfMeta.get("targetStep")
            if targetStep is not None and targetStep != wfMeta.get("step"):
                wfChanged = True
                # Step change
                wfMeta.set("step", targetStep)
                wfMeta.removePath("targetStep")

            # This must be a re-index then
            else:
                targetStep = wfMeta.get("step")

            # Security change
            stages = self.config.getJsonList("stages")
            for stage in stages:
                if stage.get("name") == targetStep:
                    wfMeta.set("label", stage.get("label"))
                    self.item_security = stage.getList("visibility")
                    workflow_security = stage.getList("security")
                    if wfChanged == True:
                        self.message_list = stage.getList("message")

            # Form processing
            formData = wfMeta.getJsonList("formData")
            if formData.size() > 0:
                formData = formData[0]
            else:
                formData = None
            coreFields = ["title", "creator", "contributor", "description", "format", "creationDate"]
            if formData is not None:
                # Core fields
                title = formData.getList("title")
                if title:
                    self.titleList = title
                creator = formData.getList("creator")
                if creator:
                    self.creatorList = creator
                contributor = formData.getList("contributor")
                if contributor:
                    self.contributorList = contributor
                description = formData.getList("description")
                if description:
                    self.descriptionList = description
                format = formData.getList("format")
                if format:
                    self.formatList = format
                creation = formData.getList("creationDate")
                if creation:
                    self.creationDate = creation
                # Non-core fields
                data = formData.getMap("/")
                for field in data.keySet():
                    if field not in coreFields:
                        self.customFields[field] = formData.getList(field)

        except StorageException, e:
            # No workflow payload, time to create
            wfChanged = True
            wfMeta = JsonConfigHelper()
            wfMeta.set("id", WORKFLOW_ID)
            wfMeta.set("step", "pending")
            wfMeta.set("pageTitle", "Uploaded Files - Management")
            stages = self.config.getJsonList("stages")
            for stage in stages:
                if stage.get("name") == "pending":
                    wfMeta.set("label", stage.get("label"))
                    self.item_security = stage.getList("visibility")
                    workflow_security = stage.getList("security")
                    self.message_list = stage.getList("message")
예제 #19
0
            # Step change
            wfMeta.set("step", targetStep)
            wfMeta.removePath("targetStep")

        # This must be a re-index then
        else:
            targetStep = wfMeta.get("step")

        # Security change
        stages = jsonConfig.getJsonList("stages")
        for stage in stages:
            if stage.get("name") == targetStep:
                item_security = stage.getList("visibility")
                workflow_security = stage.getList("security")
        # Form processing
        formData = wfMeta.getJsonList("formData")
        if formData.size() > 0:
            formData = formData[0]
        else:
            formData = None
        coreFields = ["title", "creator", "contributor", "description", "format", "creationDate"]
        if formData is not None:
            # Core fields
            title = formData.getList("title")
            if title is not None and title.size() > 0:
                titleList = title
            creator = formData.getList("creator")
            if creator is not None and creator.size() > 0:
                creatorList = creator
            contributor = formData.getList("contributor")
            if contributor is not None and contributor.size() > 0:
예제 #20
0
 def __init__(self):
     print "formData=%s" % formData
     func = formData.get("func")
     result = "{}"
     resultType = "text/plain; charset=UTF-8"
     oid = formData.get("oid")
     portalId = formData.get("portalId")
     portalManager = Services.getPortalManager()
     if func == "reharvest":
         if oid:
             print "Reharvesting single object: %s" % oid
             portalManager.reharvest(oid)
             result = '{ status: "ok" }'
         elif portalId:
             portal = portalManager.get(portalId)
             print " Reharvesting portal: %s" % portal.getName()
             indexer = Services.getIndexer()
             # TODO security filter
             # TODO this should loop through the whole portal,
             #      not just the first page of results
             if portal.getQuery() == "":
                 searchRequest = SearchRequest("item_type:object")
             else:
                 searchRequest = SearchRequest(portal.getQuery())
             result = ByteArrayOutputStream();
             Services.getIndexer().search(searchRequest, result)
             json = JsonConfigHelper(ByteArrayInputStream(result.toByteArray()))
             objectIds = HashSet()
             for doc in json.getJsonList("response/docs"):
                 objectIds.add(doc.get("id"))
             if not objectIds.isEmpty():
                 portalManager.reharvest(objectIds)
             result = '{ status: "ok" }'
         else:
             result = '{ status: "failed" }'
     elif func == "get-state":
         result = '{ running: "%s", lastResult: "%s" }' % \
             (sessionState.get("reharvest/running"),
              sessionState.get("reharvest/lastResult"))
     elif func == "get-log":
         context = LoggerFactory.getILoggerFactory()
         logger = context.getLogger("au.edu.usq.fascinator.HarvestClient")
         appender = logger.getAppender("CYCLIC")
         layout = HTMLLayout()
         layout.setContext(context)
         layout.setPattern("%d%msg")
         layout.setTitle("Reharvest log")
         layout.start()
         result = "<table>"
         count = appender.getLength()
         if count == -1:
             result += "<tr><td>Failed</td></tr>"
         elif count == 0:
             result += "<tr><td>No logging events</td></tr>"
         else:
             for i in range(0, count):
                 event = appender.get(i)
                 result += layout.doLayout(event)
         result += "</table>"
         resultType = "text/html; charset=UTF-8"
     writer = response.getPrintWriter(resultType)
     writer.println(result)
     writer.close()