<commit_before>/*
    Copyright 2015 Australian National Botanic Gardens

    This file is part of NSL services project.

    Licensed under the Apache License, Version 2.0 (the "License"); you may not
    use this file except in compliance with the License. You may obtain a copy
    of the License at http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

package au.org.biodiversity.nsl


import grails.plugins.rest.client.RestResponse
import grails.transaction.Transactional
import org.codehaus.groovy.grails.web.json.JSONArray
import org.grails.plugins.metrics.groovy.Timed
import org.springframework.cache.Cache
import org.springframework.cache.CacheManager

@Transactional
class LinkService {
    def restCallService
    def grailsApplication
    CacheManager grailsCacheManager

    @Timed()
    ArrayList<Map> getLinksForObject(target) {
        return doUsingCache(getLinksCache(), target?.id) {
            try {
                String url = getLinkServiceUrl(target, 'links', true)
                if (url) {
                    RestResponse response = restCallService.nakedGet(url)
                    if (response.status == 200) {
                        return new ArrayList<Map>(response.json as ArrayList)
                    }
                    if (response.status == 404) {
                        String link = addTargetLink(target)
                        if (link) {
                            return [[link: link, resourceCount: 1, preferred: true]]  as ArrayList<Map>
                        } else {
                            log.error "Links not found for $target, and couldn't be added."
                            return []
                        }
                    }
                    log.error "Couldn't get links for $target. $response.status: $response.json"
                    return []
                }
            } catch (Exception e) {
                log.error(e.message)
            }<commit_msg>Try to avoid a JSON result from get links for caching by creating a new ArrayList<Map><commit_after>/*
    Copyright 2015 Australian National Botanic Gardens

    This file is part of NSL services project.

    Licensed under the Apache License, Version 2.0 (the "License"); you may not
    use this file except in compliance with the License. You may obtain a copy
    of the License at http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

package au.org.biodiversity.nsl


import grails.plugins.rest.client.RestResponse
import grails.transaction.Transactional
import org.codehaus.groovy.grails.web.json.JSONArray
import org.grails.plugins.metrics.groovy.Timed
import org.springframework.cache.Cache
import org.springframework.cache.CacheManager

@Transactional
class LinkService {
    def restCallService
    def grailsApplication
    CacheManager grailsCacheManager

    @Timed()
    ArrayList<Map> getLinksForObject(target) {
        return doUsingCache(getLinksCache(), target?.id) {
            try {
                String url = getLinkServiceUrl(target, 'links', true)
                if (url) {
                    RestResponse response = restCallService.nakedGet(url)
                    if (response.status == 200) {
                        //copy the json response objects to a real ArrayList<Map> which is serializable
                        ArrayList<Map> links = []
                        ArrayList responseLinks = response.json as ArrayList
                        responseLinks.each {Map linkMap ->
                            links.add(new LinkedHashMap(linkMap))
                        }
                        return links
                    }
                    if (response.status == 404) {
                        String link = addTargetLink(target)
                        if (link) {
                            return [[link: link, resourceCount: 1, preferred: true]]  as ArrayList<Map>
                        } else {
                            log.error "Links not found for $target, and couldn't be added."
                            return []
                        }
                    }
                    log.error "Couldn't get links for $target. $response.status: $response.json"
                    return []
                }
            } catch (Exception e) {
                log.error(e.message)
            }
{'fraction_non_alphanumeric': 0.06909090909090909, 'fraction_numerical': 0.006060606060606061, 'mean_word_length': 1.8146674246731098, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3138720', 'n_tokens_mistral': 1211, 'n_tokens_neox': 1130, 'n_words': 402}