<commit_before>/**
 * A RESTful web service on top of DSpace.
 * Copyright (C) 2010-2014 National Library of Finland
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

package fi.helsinki.lib.simplerest;

import fi.helsinki.lib.simplerest.stubs.StubItem;
import com.google.gson.Gson;
import fi.helsinki.lib.simplerest.options.GetOptions;
import java.sql.SQLException;
import java.text.Normalizer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;

import org.dspace.core.Context;
import org.dspace.content.Collection;
import org.dspace.content.Item;
import org.dspace.content.Bundle;
import org.dspace.content.DCValue;

import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.EmptyRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.InputRepresentation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.Put; 
import org.restlet.resource.Post;
import org.restlet.resource.Delete;
import org.restlet.resource.ResourceException;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.data.Form;

import org.restlet.util.Series;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import org.dspace.authorize.AuthorizeException;

public class ItemResource extends BaseResource {

    private static Logger log = Logger.getLogger(ItemResource.class);
    
    private int itemId = -1;
    private Item item;
    private Context context;
    private Date ifModifiedSince = new Date();
    
    public ItemResource(Item i, int itemId){
        this.item = i;
        this.itemId = itemId;
    }
    
    public ItemResource(){
        this.itemId = 0;
        this.item = null;
        try{
            this.context = new Context();
        }catch(SQLException e){
            log.log(Priority.FATAL, e);
        }
    }

    static public String relativeUrl(int itemId) {
        return "item/" + itemId;
    }

    
    @Override
    protected void doInit() throws ResourceException {
        try {
            String s = (String)getRequest().getAttributes().get("itemId");
            this.itemId = Integer.parseInt(s);
        }
        catch (NumberFormatException e) {
            ResourceException resourceException =
                new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
                                      "Item ID must be a number.");
            throw resourceException;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "EEE, dd MMM yyyy HH:mm:ss z");
        Series headers = (Series)getRequestAttributes().get("org.restlet.http.headers");
        String date = headers.getFirstValue("If-Modified-Since");
        try{
            if(date != null) {
                this.ifModifiedSince = dateFormat.parse(date);
            }
        }catch(ParseException e){
            ResourceException resourceException =
                    new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "If-Modified-Since must be date");
        }
    }

    @Get("html|xhtml|xml")
    public Representation toXml() {<commit_msg>fixes to modified logic<commit_after>/**
 * A RESTful web service on top of DSpace.
 * Copyright (C) 2010-2014 National Library of Finland
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

package fi.helsinki.lib.simplerest;

import fi.helsinki.lib.simplerest.stubs.StubItem;
import com.google.gson.Gson;
import fi.helsinki.lib.simplerest.options.GetOptions;
import java.sql.SQLException;
import java.text.Normalizer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;

import org.dspace.core.Context;
import org.dspace.content.Collection;
import org.dspace.content.Item;
import org.dspace.content.Bundle;
import org.dspace.content.DCValue;

import org.restlet.engine.util.DateUtils;
import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.EmptyRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.InputRepresentation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.Put; 
import org.restlet.resource.Post;
import org.restlet.resource.Delete;
import org.restlet.resource.ResourceException;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.data.Form;

import org.restlet.util.Series;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import org.dspace.authorize.AuthorizeException;

public class ItemResource extends BaseResource {

    private static Logger log = Logger.getLogger(ItemResource.class);
    
    private int itemId = -1;
    private Item item;
    private Context context;
    private Date ifModifiedSince = new Date();
    
    public ItemResource(Item i, int itemId){
        this.item = i;
        this.itemId = itemId;
    }
    
    public ItemResource(){
        this.itemId = 0;
        this.item = null;
        try{
            this.context = new Context();
        }catch(SQLException e){
            log.log(Priority.FATAL, e);
        }
    }

    static public String relativeUrl(int itemId) {
        return "item/" + itemId;
    }

    
    @Override
    protected void doInit() throws ResourceException {
        try {
            String s = (String)getRequest().getAttributes().get("itemId");
            this.itemId = Integer.parseInt(s);
        }
        catch (NumberFormatException e) {
            ResourceException resourceException =
                new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
                                      "Item ID must be a number.");
            throw resourceException;
        }

        Series headers = (Series)getRequestAttributes().get("org.restlet.http.headers");
        String date = headers.getFirstValue("If-Modified-Since");
        if(date != null) {
            this.ifModifiedSince = DateUtils.parse(date);
        }
    }

    @Get("html|xhtml|xml")
    public Representation toXml() {
{'fraction_non_alphanumeric': 0.09342156481121058, 'fraction_numerical': 0.004671078240560529, 'mean_word_length': 3.1351931330472103, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2889528', 'n_tokens_mistral': 2223, 'n_tokens_neox': 2165, 'n_words': 604}