package dataObjects;

import utilities.Tokenizer;

import javax.swing.*;
import java.util.HashMap;

/**
 * Created by Thagus on 03/09/16.
 */
public class Document implements Comparable<Document>{
    private int idDoc;
    private String title;
    private String journal;
    private String libraryNotes;
    private String authors;
    private String abstractText;
    private String keywords;
    private String classification;
    private String citations;

    private double similarity;

    public Document(int idDoc){
        this.idDoc = idDoc;
        title = "";
    }

    //Count terms while adding?

    public void appendTitle(String string){
        title += " " + string.trim();
        title = title.trim();
        similarity = 0;
    }

    public void appendAuthor(String author){
        if(authors==null){
            authors = author.trim();
        } else {
            authors = authors + "; " + author.trim();
        }
    }

    public void setJournal(String journal){
        if(this.journal==null) {
            this.journal = journal.trim();
        }
        else {
            JOptionPane.showMessageDialog(null, "There is more than one journal entry for document idDoc: " + idDoc);
        }
    }

    public void setLibraryNotes(String libraryNotes){
        if(this.libraryNotes ==null)
            this.libraryNotes = libraryNotes;
        else {
            JOptionPane.showMessageDialog(null, "There is more than one library note entry for document idDoc: " + idDoc);
        }
    }

    public void appendAbstract(String text){
        if(abstractText ==null){
            abstractText = text.trim();
        } else {
            abstractText += " " + text.trim();
        }
    }

    public void appendKeywords(String keywords) {
        if(this.keywords == null){
            this.keywords = keywords.trim();
        } else {
            this.keywords += " " + keywords.trim();
        }
    }

    public void appendClassification(String classification) {
        if(this.classification == null){
            this.classification = classification.trim();
        } else {
            this.classification += "; " + classification.trim();
        }
    }

    public void appendCitations(String citations) {
        if(this.citations == null){
            this.citations = citations.trim();
        }
        else{
            this.citations += "; " + citations.trim();
        }
    }

    public void setSimilarity(double similarity) {
        this.similarity = similarity;
    }

    public int getIdDoc() { return idDoc; }

    public String getTitle() {
        return title;
    }

    public String getJournal() {
        return journal;
    }

    public String getLibraryNotes() { return libraryNotes; }

    public String getAuthors(){
        return authors;
    }

    public String getAbstractText() {
        return abstractText;
    }

    public String getKeywords() {
        return keywords;
    }

    public String getClassification() {
        return classification;
    }

    public String getCitations() {
        return citations;
    }

    public double getSimilarity() {
        return similarity;
    }

    public HashMap<String, Integer> countWords(HashMap<String, Integer> documentWordOccurrence){
        String text;
        if(abstractText!=null)
            text = title + " " + abstractText;
        else
            text = title;

        String[] splited = Tokenizer.tokenizeString(text);

        HashMap<String, Integer> wordCountLocal = new HashMap<>();

        for(String word : splited){
            if(word.length()<=2)    //Filter void(0) words, and those with length 1 or 2
                continue;

            //Local count
            Integer count = wordCountLocal.get(word);
            if(count==null){
                count = 0;

                //When first counting the word, add it to the global document word count
                //Global document word count
                Integer countGl = documentWordOccurrence.get(word);

                if(countGl==null){
                    countGl = 0;
                }
                documentWordOccurrence.put(word, countGl+1);
            }
            wordCountLocal.put(word, count+1);
        }

        return wordCountLocal;
    }

    @Override
    public int compareTo(Document o) {
        if(this.similarity == o.similarity)
            return 0;
        else if (this.similarity<o.similarity)
            return -1;
        else
            return 1;
    }
}

@@ -579,41 +579,8 @@
  %7D%0A%0A
-    //Count terms while adding?%0A%0A
     
@@ -619,16 +619,16 @@
 tring)%7B%0A
+
         
@@ -763,16 +763,63 @@
 uthor)%7B%0A
+        if(author == null)%0A            return;%0A
         
@@ -1029,18 +1029,20 @@
 .journal
+ 
 ==
+ 
 null) %7B%0A
@@ -1329,16 +1329,17 @@
 Notes ==
+ 
 null)%0A  
@@ -1582,16 +1582,59 @@
  text)%7B%0A
+        if(text==null)%0A            return;%0A
         
@@ -1825,24 +1825,71 @@
 keywords) %7B%0A
+        if(keywords==null)%0A            return;%0A
         if(t
@@ -2100,24 +2100,77 @@
 fication) %7B%0A
+        if(classification==null)%0A            return;%0A
         if(t
@@ -2346,32 +2346,32 @@
        %7D%0A    %7D%0A%0A
-
     public void 
@@ -2402,24 +2402,72 @@
 itations) %7B%0A
+        if(citations==null)%0A            return;%0A
         if(t

Handle null values