001    // This file is part of AceWiki.
002    // Copyright 2008-2012, AceWiki developers.
003    // 
004    // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU
005    // Lesser General Public License as published by the Free Software Foundation, either version 3 of
006    // the License, or (at your option) any later version.
007    // 
008    // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009    // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010    // Lesser General Public License for more details.
011    // 
012    // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If
013    // not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.acewiki.core;
016    
017    import java.util.ArrayList;
018    import java.util.List;
019    import java.util.Vector;
020    
021    /**
022     * This class represents a wiki article.
023     * 
024     * @author Tobias Kuhn
025     */
026    public class Article {
027            
028            private Vector<Statement> statements = new Vector<Statement>();
029            private final OntologyElement element;
030            private final Ontology ontology;
031            
032            /**
033             * Creates a new article for the given ontology element.
034             * 
035             * @param element The ontology element.
036             */
037            protected Article(OntologyElement element) {
038                    this.element = element;
039                    this.ontology = element.getOntology();
040            }
041            
042            void initStatements(List<Statement> statements) {
043                    this.statements = new Vector<Statement>(statements);
044            }
045            
046            /**
047             * Returns the ontology element of this article.
048             * 
049             * @return The ontology element.
050             */
051            public OntologyElement getOntologyElement() {
052                    return element;
053            }
054            
055            /**
056             * Returns the ontology object.
057             * 
058             * @return The ontoloy.
059             */
060            public Ontology getOntology() {
061                    return ontology;
062            }
063            
064            /**
065             * Returns the article text as a list of statements.
066             * 
067             * @return The statements.
068             */
069            public List<Statement> getStatements() {
070                    return new ArrayList<Statement>(statements);
071            }
072            
073            /**
074             * Returns the ACE sentences of the article text.
075             * 
076             * @return The ACE sentences.
077             */
078            public List<Sentence> getSentences() {
079                    List<Sentence> sentences = new ArrayList<Sentence>();
080                    for (Statement s : statements) {
081                            if (s instanceof Sentence) {
082                                    sentences.add((Sentence) s);
083                            }
084                    }
085                    return sentences;
086            }
087            
088            /**
089             * Edits a statement of the article. The old statement is replaced by the new statement.
090             * 
091             * @param oldStatement The statement that should be edited.
092             * @param newStatement The new statement.
093             */
094            public void edit(Statement oldStatement, Statement newStatement) {
095                    List<Statement> newStatements = new ArrayList<Statement>();
096                    newStatements.add(newStatement);
097                    edit(oldStatement, newStatements);
098            }
099            
100            /**
101             * Edits a statement of the article. The old statement is replaced by the new statements.
102             * 
103             * @param oldStatement The statement that should be edited.
104             * @param newStatements The new statements.
105             */
106            public void edit(Statement oldStatement, List<Statement> newStatements) {
107                    log("edit statement of " + element.getWord() + ": " +
108                                    oldStatement.getText(getDefaultLanguage()) + " > " +
109                                    getStatementsString(newStatements));
110                    
111                    synchronized (ontology) {
112                            if (statements.contains(oldStatement)) {
113                                    int i = statements.indexOf(oldStatement);
114                                    statements.remove(i);
115                                    statements.addAll(i, newStatements);
116                            } else {
117                                    log("error: statement is not around anymore");
118                                    statements.addAll(0, newStatements);
119                            }
120                            if (ontology != null) {
121                                    if (oldStatement instanceof Sentence) {
122                                            ontology.retractSentence((Sentence) oldStatement);
123                                    }
124                                    for (Statement s : newStatements) {
125                                            if (s instanceof Sentence) {
126                                                    ontology.commitSentence((Sentence) s);
127                                            }
128                                    }
129                                    ontology.getStorage().save(element);
130                            }
131                    }
132            }
133            
134            /**
135             * Adds one new statement to the article. One has to specify in front of which
136             * statement the new statement should be added.
137             * 
138             * @param followingStatement The statement in front of which the new statement should be added,
139             *     or null if the statement should be added to the end of the article.
140             * @param newStatement The new statement to be added.
141             */
142            public void add(Statement followingStatement, Statement newStatement) {
143                    List<Statement> newStatements = new ArrayList<Statement>();
144                    newStatements.add(newStatement);
145                    add(followingStatement, newStatements);
146            }
147            
148            /**
149             * Adds one or more new statements to the article. It has to be specified in front of which
150             * statement the new statement should be added.
151             * 
152             * @param followingStatement The statement in front of which the new statements should be
153             *     added, or null if the statements should be added to the end of the article.
154             * @param newStatements The new statements to be added.
155             */
156            public void add(Statement followingStatement, List<Statement> newStatements) {
157                    log("add statements of " + element.getWord() + ": " + getStatementsString(newStatements));
158    
159                    synchronized (ontology) {
160                            if (statements.contains(followingStatement)) {
161                                    statements.addAll(statements.indexOf(followingStatement), newStatements);
162                            } else {
163                                    if (followingStatement != null) {
164                                            log("error: statement is not around anymore");
165                                    }
166                                    statements.addAll(newStatements);
167                            }
168                            if (ontology != null) {
169                                    for (Statement s : newStatements) {
170                                            if (s instanceof Sentence) {
171                                                    ontology.commitSentence((Sentence) s);
172                                            }
173                                    }
174                                    ontology.getStorage().save(element);
175                            }
176                    }
177            }
178            
179            private String getStatementsString(List<Statement> statements) {
180                    String result = "";
181                    for (Statement s : statements) {
182                            result += s.getText(getDefaultLanguage()) + " ";
183                    }
184                    return result;
185            }
186            
187            /**
188             * Removes the given statement from the article.
189             * 
190             * @param statement The statement to be removed.
191             */
192            public void remove(Statement statement) {
193                    synchronized (ontology) {
194                            if (statements.contains(statement)) {
195                                    log("remove statement: " + statement.getText(getDefaultLanguage()));
196                                    statements.remove(statement);
197                            }
198                            if (ontology != null) {
199                                    if (statement instanceof Sentence) {
200                                            ontology.retractSentence((Sentence) statement);
201                                    }
202                                    ontology.getStorage().save(element);
203                            }
204                    }
205            }
206            
207            /**
208             * Writes the text to the log file.
209             * 
210             * @param text The text to be written to the log file.
211             */
212            protected void log(String text) {
213                    if (ontology != null) {
214                            ontology.log(text);
215                    }
216            }
217            
218            private String getDefaultLanguage() {
219                    return getOntology().getEngine().getLanguages()[0];
220            }
221    
222    }