package app.database.model.note;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.ytreza.data.Filter;
import org.ytreza.data.ModelWithOrigin;
import org.ytreza.data.model.modelField.StringField;
import org.ytreza.data.table.exception.ItemException;
import org.ytreza.tool.StringUtilities;
import org.ytreza.tool.internationalization.Tr;
import app.database.model.ModelName;
import app.database.model.tag.TableTag;
import app.database.model.tag.ModelTag;
import app.database.model.tagAndNote.TableTagForNote;
import app.database.model.tagAndNote.ModelTagAndNote;
public class ModelNote extends ModelWithOrigin<ModelNote>{
static final String SUBJECT = "subject";
static final String TEXT = "text";
private StringField subject , text;
private TableTagForNote tableTagAndNote;
private TableTag tableTag;
private String initialSubject = null;
private String initialText = null;
private String initialTagList = null;
private List<ModelTag> tagList;
private List<ModelTag> tagOfficialList;
private boolean hasNoTag;
public ModelNote() {}
public ModelNote(String subject) {
initialSubject = subject;
}
public ModelNote(String subject, String text) {
this(subject);
initialText = text;
}
public ModelNote(String subject, String text, String tagList) {
this(subject,text);
initialTagList = tagList;
}
public ModelNote(String subject, String text, List<ModelTag> tagList) {
this(subject, text);
List<String> list = new ArrayList<String>();
for (ModelTag tag : tagList) {
list.add(tag.getFullLabel());
}
initialTagList = StringUtilities.Join(list, ";");
}
@Override public boolean canBeDeleted() {return true;}
@Override public boolean canBeInserted() {
return (! subject.getValue().equals(""));
}
@Override public boolean canBeUpdated() {
return (! subject.getValue().equals(""));
}
@Override
public void initializeFields() {
subject = (StringField) addField(new StringField(SUBJECT));
text = (StringField) addField(new StringField(TEXT));
if (initialSubject != null) {
setSubject(initialSubject);
}
if (initialText!= null) {
setText(initialText);
}
if (initialTagList!= null) {
setTagList(initialTagList);
}
}
public void setSubject(String subject) {this.subject.setValue(subject);}
public void setText(String text) {this.text.setValue(text);}
public String getSubject() {return subject.getValue();}
public String getText() {return text.getValue();}
public void addTag(String tagLabel) {
addTag(createTag(tagLabel));
}
public void setTagList(String tagList) {
List<ModelTag> newTagList = createTagListFromString(tagList);
setTagList(newTagList);
}
public void setTagList(List<ModelTag> tagList) {
if (! (tagList.containsAll(getTagList()) && getTagList().containsAll(tagList))) {
removeAllTag();
for (ModelTag tag : tagList) {
addTag(tag);
}
}
}
private List<ModelTag> createTagListFromString(String tagList) {
List<ModelTag> result = new ArrayList<ModelTag>();
tagList = tagList.replace('\n',';');
List<String> list = Arrays.asList(tagList.split(";"));
for (String label : list) {
result.add(createTag(label));
}
return result;
}
public boolean hasTag(ModelTag tag) {
createTagListIfNecessary();
return tagList.contains(tag);
}
private void createTagListIfNecessary(){
if (tagList == null || tagOfficialList == null) {
resetTagList();
fillTagList();
}
addNoTag();
}
private void addNoTag() {
if (tagList.isEmpty()) {
hasNoTag = true;
ModelTag noTag = createTag(Tr._("no_tag"));
tagList.add(noTag);
tagOfficialList.add(noTag);
}
}
public void addTag(ModelTag tag) {
createTagListIfNecessary();
if (! tag.isEmpty() && ! tagOfficialList.contains(tag)) {
removeNoTag();
tagOfficialList.add(tag);
addUnofficialTag(tag);
markUpdated();
}
}
private void addUnofficialTag(ModelTag tag) {
if (! tagList.contains(tag)) {
tagList.add(tag);
if (! tag.getParent().isNull()) {
addUnofficialTag(tag.getParent());
}
}
}
private void removeNoTag() {
if (hasNoTag) {
hasNoTag = false;
ModelTag noTag = createTag(Tr._("no_tag"));
tagList.remove(noTag);
tagOfficialList.remove(noTag);
}
}
private void fillTagList() {
try {
Filter filter = new Filter();
filter.add(ModelTagAndNote.NAME_OBJECT_ID, getId());
for (ModelTagAndNote tagAndNote : getTableTagAndNote().getItem(filter)) {
ModelTag tag = tagAndNote.getTag();
if (! tag.isEmpty()) {
if (tagAndNote.isOfficial()) {
tagOfficialList.add(tagAndNote.getTag());
}
tagList.add(tagAndNote.getTag());
}
}
} catch (ItemException e) {
// AUTO_TODO Auto-generated catch block
e.printStackTrace();
}
}
private void resetTagList() {
tagList = new ArrayList<ModelTag>();
tagOfficialList = new ArrayList<ModelTag>();
}
protected TableTagForNote getTableTagAndNote() {
if (tableTagAndNote == null) {
// tableTagAndNote = getTableFactory().getTable(tableName);
}
return tableTagAndNote;
}
protected TableTag getTableTag() {
if (tableTag == null) {
tableTag = (TableTag) getTableFactory().getTable(ModelName.TAG);
}
return tableTag;
}
public void removeTag(String tagLabel) {
if (hasTag(tagLabel)) {
tagList.remove(createTag(tagLabel));
}
}
private ModelTag createTag(String tagLabel) {
return getTableTag().createItem(new ModelTag(tagLabel));
}
public boolean hasTag(String tagLabel) {
return hasTag(createTag(tagLabel));
}
public void removeAllTag() {
if (tagList != null) {
tagList.clear();
} else {
tagList = new ArrayList<ModelTag>();
}
}
public List<ModelTag> getTagList() {
createTagListIfNecessary();
return tagList;
}
public List<ModelTag> getOfficialTagList() {
createTagListIfNecessary();
return tagOfficialList;
}
public String toString() {
return getSubject();
}
@Override
public ModelNote copy() {
return new ModelNote(getSubject(), getText(), getTagList());
}
public boolean hasTag(List<ModelTag> list) {
createTagListIfNecessary();
return tagList.containsAll(list);
}
}