All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.anotheria.anosite.gen.asblogdata.service.ASBlogDataServiceImpl Maven / Gradle / Ivy

There is a newer version: 4.1.2
Show newest version
/**
 ********************************************************************************
 *** ASBlogDataServiceImpl.java                                               ***
 *** The implementation of the IASBlogDataService.                            ***
 *** generated by AnoSiteGenerator (ASG), Version: 2.6.3                      ***
 *** Copyright (C) 2005 - 2010 Anotheria.net, www.anotheria.net               ***
 *** All Rights Reserved.                                                     ***
 ********************************************************************************
 *** Don't edit this code, if you aren't sure                                 ***
 *** that you do exactly know what you are doing!                             ***
 *** It's better to invest time in the generator, as into the generated code. ***
 ********************************************************************************
 */

package net.anotheria.anosite.gen.asblogdata.service;

import java.nio.charset.Charset;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import net.anotheria.anodoc.data.Module;
import net.anotheria.anodoc.data.Property;
import net.anotheria.anodoc.data.NoSuchPropertyException;
import net.anotheria.util.sorter.SortType;
import net.anotheria.util.sorter.StaticQuickSorter;
import net.anotheria.util.slicer.Segment;
import net.anotheria.util.slicer.Slicer;
import net.anotheria.anosite.gen.asblogdata.data.ModuleASBlogData;
import net.anotheria.anosite.gen.shared.service.BasicCMSService;
import net.anotheria.anodoc.query2.DocumentQuery;
import net.anotheria.anodoc.query2.QueryResult;
import net.anotheria.anodoc.query2.QueryResultEntry;
import net.anotheria.anodoc.query2.QueryProperty;
import net.anotheria.util.StringUtils;
import net.anotheria.util.xml.XMLNode;
import net.anotheria.util.xml.XMLAttribute;
import net.anotheria.asg.util.listener.IModuleListener;
import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.IOException;
import net.anotheria.anodoc.util.mapper.ObjectMapperUtil;
import net.anotheria.anosite.gen.shared.util.DocumentName;
import net.anotheria.anosite.gen.asblogdata.data.Post;
import net.anotheria.anosite.gen.asblogdata.data.PostXMLHelper;
import net.anotheria.anosite.gen.asblogdata.data.PostDocument;
import net.anotheria.anosite.gen.asblogdata.service.ASBlogDataServiceException;
import net.anotheria.anosite.gen.asblogdata.data.Comment;
import net.anotheria.anosite.gen.asblogdata.data.CommentXMLHelper;
import net.anotheria.anosite.gen.asblogdata.data.CommentDocument;
import net.anotheria.anosite.gen.asblogdata.data.Tag;
import net.anotheria.anosite.gen.asblogdata.data.TagXMLHelper;
import net.anotheria.anosite.gen.asblogdata.data.TagDocument;


public class ASBlogDataServiceImpl extends BasicCMSService implements IASBlogDataService, IModuleListener{

	// Generated by: class net.anotheria.asg.generator.model.docs.CMSBasedServiceGenerator.generateImplementation

	private static ASBlogDataServiceImpl instance;

	private ASBlogDataServiceImpl(){
		addServiceListener(new net.anotheria.anosite.cms.listener.CRUDLogListener());
		addServiceListener(new net.anotheria.anosite.cms.listener.BlogListener());
		addModuleListener(ModuleASBlogData.MODULE_ID, this);
	}

	static final ASBlogDataServiceImpl getInstance(){
		if (instance==null){
			instance = new ASBlogDataServiceImpl();
		}
		return instance;
	}

	private ModuleASBlogData _getModuleASBlogData(){
		return (ModuleASBlogData) getModule(ModuleASBlogData.MODULE_ID);
	}

	@Override
	public void moduleLoaded(Module module){
		firePersistenceChangedEvent();
	}

	@SuppressWarnings("unchecked")
	public List getPosts(){
		List posts = new ArrayList();
		posts.addAll(_getModuleASBlogData().getPosts());
		return posts;
	}

	public List getPosts(SortType sortType){
		return StaticQuickSorter.sort(getPosts(), sortType);
	}

	/**
	 * Returns the Post objects with the specified ids.
	 */
	public List getPosts(List ids){;
		if (ids==null || ids.size()==0)
			return new ArrayList(0);
		List all = getPosts();
		List ret = new ArrayList();
		for (Post post : all){
			if(ids.contains(post.getId())){
				ret.add(post);
			}
		}
		return ret;
	}

	/**
	 * Returns the Post objects with the specified ids, sorted by given sorttype.
	 */
	public List getPosts(List ids, SortType sortType){;
		return StaticQuickSorter.sort(getPosts(ids), sortType);
	}

	public void deletePost(Post post){
		deletePost(post.getId());
		if (hasServiceListeners()){
			fireObjectDeletedEvent(post);
		}
	}

	public void deletePost(String id){
		ModuleASBlogData module = _getModuleASBlogData();
		Post varValue = hasServiceListeners()?module.getPost(id):null;
		module.deletePost(id);
		updateModule(module);
		if(varValue!=null){
			fireObjectDeletedEvent(varValue);
		}
	}

	public void deletePosts(List list){
		ModuleASBlogData module = _getModuleASBlogData();
		for (Post post : list){
			module.deletePost(post.getId());
		}
		updateModule(module);
		if (hasServiceListeners()){
			for (int t=0; t importPosts(List list){
		ModuleASBlogData module = _getModuleASBlogData();
		List ret = new ArrayList();
		for (Post post : list){
			Post imported = module.importPost((PostDocument)post);
			ret.add(imported);
		}
		updateModule(module);
		if (hasServiceListeners()){
			for (Post post : ret)
				fireObjectImportedEvent(post);
		}
		return ret;
	}

	public Post createPost(Post post){
		ModuleASBlogData module = _getModuleASBlogData();
		module.createPost((PostDocument)post);
		updateModule(module);
		fireObjectCreatedEvent(post);
		return post;
	}

	/**
	 * Creates multiple new Post objects.
	 * Returns the created versions.
	 */
	public List createPosts(List list){;
		ModuleASBlogData module = _getModuleASBlogData();
		List ret = new ArrayList();
		for (Post post : list){
			Post created = module.createPost((PostDocument)post);
			ret.add(created);
		}
		updateModule(module);
		if (hasServiceListeners()){
			for (Post post : ret)
				fireObjectCreatedEvent(post);
		}
		return ret;
	}

	public Post updatePost(Post post){
		Post oldVersion = null;
		ModuleASBlogData module = _getModuleASBlogData();
		if (hasServiceListeners())
			oldVersion = module.getPost(post.getId());
		module.updatePost((PostDocument)post);
		updateModule(module);
		if (oldVersion != null){
			fireObjectUpdatedEvent(oldVersion, post);
		}
		return post;
	}

	public List updatePosts(List list){
		List oldList = null;
		if (hasServiceListeners())
			oldList = new ArrayList(list.size());
		ModuleASBlogData module = _getModuleASBlogData();
		for (Post post : list){
			if (oldList!=null)
				oldList.add(module.getPost(post.getId()));
			module.updatePost((PostDocument)post);
		}
		updateModule(module);
		if (oldList!=null){
			for (int t=0; t getPostsByProperty(String propertyName, Object value){
		List allPosts = getPosts();
		List ret = new ArrayList();
		for (int i=0; i getPostsByProperty(String propertyName, Object value, SortType sortType){
		return StaticQuickSorter.sort(getPostsByProperty(propertyName, value), sortType);
	}
	/**
	 * Executes a query on Posts
	 */
	public QueryResult executeQueryOnPosts(DocumentQuery query){
		List allPosts = getPosts();
		QueryResult result = new QueryResult();
		for (int i=0; i partialResult = query.match(allPosts.get(i));
			result.add(partialResult);
		}
		return result;
	}

	/**
	 * Returns all Post objects, where property matches.
	 */
	public List getPostsByProperty(QueryProperty... property){
		//first the slow version, the fast version is a todo.
		List ret = new ArrayList();
		List src = getPosts();
		for ( Post post : src){;
			boolean mayPass = true;
			for (QueryProperty qp : property){;
				mayPass = mayPass && qp.doesMatch(post.getPropertyValue(qp.getName()));
			}
			if (mayPass)
				ret.add(post);
		}
		return ret;
	}

	/**
	 * Returns all Post objects, where property matches, sorted
	 */
	public List getPostsByProperty(SortType sortType, QueryProperty... property){
		return StaticQuickSorter.sort(getPostsByProperty(property), sortType);
	}

	/**
	 * Returns Post objects count.
	 */
	public int getPostsCount() {
		return _getModuleASBlogData().getPosts().size();
	}

	/**
	 * Returns Post objects segment.
	 */
	public List getPosts(Segment aSegment) {
		return Slicer.slice(aSegment, getPosts()).getSliceData();
	}

	/**
	 * Returns Post objects segment, where property matched.
	 */
	public List getPostsByProperty(Segment aSegment, QueryProperty... property) {
		int pLimit = aSegment.getElementsPerSlice();
		int pOffset = aSegment.getSliceNumber() * aSegment.getElementsPerSlice() - aSegment.getElementsPerSlice();
		List ret = new ArrayList();
		List src = getPosts();
		for (Post post : src) {;
			boolean mayPass = true;
			for (QueryProperty qp : property) {;
				mayPass = mayPass && qp.doesMatch(post.getPropertyValue(qp.getName()));
			}
			if (mayPass)
				ret.add(post);
			if (ret.size() > pOffset + pLimit)
				break;
		}
		return Slicer.slice(aSegment, ret).getSliceData();
	}

	/**
	 * Returns Post objects segment, where property matched, sorted.
	 */
	public List getPostsByProperty(Segment aSegment, SortType aSortType, QueryProperty... aProperty){
		return StaticQuickSorter.sort(getPostsByProperty(aSegment, aProperty), aSortType);
	}

	@Override
	public void fetchPost(final String id, Set addedDocuments, JSONArray data) throws ASBlogDataServiceException {
		if (id.isEmpty() || addedDocuments.contains("Post" + id))
			return;

		try {
			final PostDocument post = _getModuleASBlogData().getPost(id);
			addedDocuments.add("Post" + id);


			if (!post.getComments().isEmpty()) {
				for (String aCommentsId: post.getComments()) {
					getASBlogDataService().fetchComment(aCommentsId, addedDocuments, data);
				}
			}
			if (!post.getTags().isEmpty()) {
				for (String aTagsId: post.getTags()) {
					getASBlogDataService().fetchTag(aTagsId, addedDocuments, data);
				}
			}

			JSONObject dataObject = new JSONObject();
			String jsonObject = ObjectMapperUtil.getMapperInstance().writeValueAsString(post);
			dataObject.put("object", jsonObject);
			dataObject.put("service", "ASBlogData");
			dataObject.put("document", "ASBlogData_Post");

			data.put(dataObject);

		} catch (ASBlogDataServiceException e) {
			throw new ASBlogDataServiceException("Problem with getting document from ASBlogData" + e.getMessage());
		} catch (JsonProcessingException e) {
			throw new ASBlogDataServiceException ("Problem with fetching data for this Post instance object:" + e.getMessage());
		} catch (JSONException e) {
			throw new ASBlogDataServiceException ("Problem with fetching data for this Post instance in json :" + e.getMessage());
		}
	}

	private void saveTransferredPost(final JSONObject data) throws ASBlogDataServiceException  {
		try {
			String objectData = data.getString("object");
			Post post = ObjectMapperUtil.getMapperInstance().readValue(objectData.getBytes(Charset.forName("UTF-8")), PostDocument.class);

			try {
				updatePost(post);
			} catch (Exception e) {
				importPost(post);
			}
		} catch (JSONException e) {
			throw new ASBlogDataServiceException ("Problem with getting data from json Post instance :" + e.getMessage());
		} catch (IOException e) {
			throw new ASBlogDataServiceException ("Problem with parsing data for this Post instance :" + e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	public List getComments(){
		List comments = new ArrayList();
		comments.addAll(_getModuleASBlogData().getComments());
		return comments;
	}

	public List getComments(SortType sortType){
		return StaticQuickSorter.sort(getComments(), sortType);
	}

	/**
	 * Returns the Comment objects with the specified ids.
	 */
	public List getComments(List ids){;
		if (ids==null || ids.size()==0)
			return new ArrayList(0);
		List all = getComments();
		List ret = new ArrayList();
		for (Comment comment : all){
			if(ids.contains(comment.getId())){
				ret.add(comment);
			}
		}
		return ret;
	}

	/**
	 * Returns the Comment objects with the specified ids, sorted by given sorttype.
	 */
	public List getComments(List ids, SortType sortType){;
		return StaticQuickSorter.sort(getComments(ids), sortType);
	}

	public void deleteComment(Comment comment){
		deleteComment(comment.getId());
		if (hasServiceListeners()){
			fireObjectDeletedEvent(comment);
		}
	}

	public void deleteComment(String id){
		ModuleASBlogData module = _getModuleASBlogData();
		Comment varValue = hasServiceListeners()?module.getComment(id):null;
		module.deleteComment(id);
		updateModule(module);
		if(varValue!=null){
			fireObjectDeletedEvent(varValue);
		}
	}

	public void deleteComments(List list){
		ModuleASBlogData module = _getModuleASBlogData();
		for (Comment comment : list){
			module.deleteComment(comment.getId());
		}
		updateModule(module);
		if (hasServiceListeners()){
			for (int t=0; t importComments(List list){
		ModuleASBlogData module = _getModuleASBlogData();
		List ret = new ArrayList();
		for (Comment comment : list){
			Comment imported = module.importComment((CommentDocument)comment);
			ret.add(imported);
		}
		updateModule(module);
		if (hasServiceListeners()){
			for (Comment comment : ret)
				fireObjectImportedEvent(comment);
		}
		return ret;
	}

	public Comment createComment(Comment comment){
		ModuleASBlogData module = _getModuleASBlogData();
		module.createComment((CommentDocument)comment);
		updateModule(module);
		fireObjectCreatedEvent(comment);
		return comment;
	}

	/**
	 * Creates multiple new Comment objects.
	 * Returns the created versions.
	 */
	public List createComments(List list){;
		ModuleASBlogData module = _getModuleASBlogData();
		List ret = new ArrayList();
		for (Comment comment : list){
			Comment created = module.createComment((CommentDocument)comment);
			ret.add(created);
		}
		updateModule(module);
		if (hasServiceListeners()){
			for (Comment comment : ret)
				fireObjectCreatedEvent(comment);
		}
		return ret;
	}

	public Comment updateComment(Comment comment){
		Comment oldVersion = null;
		ModuleASBlogData module = _getModuleASBlogData();
		if (hasServiceListeners())
			oldVersion = module.getComment(comment.getId());
		module.updateComment((CommentDocument)comment);
		updateModule(module);
		if (oldVersion != null){
			fireObjectUpdatedEvent(oldVersion, comment);
		}
		return comment;
	}

	public List updateComments(List list){
		List oldList = null;
		if (hasServiceListeners())
			oldList = new ArrayList(list.size());
		ModuleASBlogData module = _getModuleASBlogData();
		for (Comment comment : list){
			if (oldList!=null)
				oldList.add(module.getComment(comment.getId()));
			module.updateComment((CommentDocument)comment);
		}
		updateModule(module);
		if (oldList!=null){
			for (int t=0; t getCommentsByProperty(String propertyName, Object value){
		List allComments = getComments();
		List ret = new ArrayList();
		for (int i=0; i getCommentsByProperty(String propertyName, Object value, SortType sortType){
		return StaticQuickSorter.sort(getCommentsByProperty(propertyName, value), sortType);
	}
	/**
	 * Executes a query on Comments
	 */
	public QueryResult executeQueryOnComments(DocumentQuery query){
		List allComments = getComments();
		QueryResult result = new QueryResult();
		for (int i=0; i partialResult = query.match(allComments.get(i));
			result.add(partialResult);
		}
		return result;
	}

	/**
	 * Returns all Comment objects, where property matches.
	 */
	public List getCommentsByProperty(QueryProperty... property){
		//first the slow version, the fast version is a todo.
		List ret = new ArrayList();
		List src = getComments();
		for ( Comment comment : src){;
			boolean mayPass = true;
			for (QueryProperty qp : property){;
				mayPass = mayPass && qp.doesMatch(comment.getPropertyValue(qp.getName()));
			}
			if (mayPass)
				ret.add(comment);
		}
		return ret;
	}

	/**
	 * Returns all Comment objects, where property matches, sorted
	 */
	public List getCommentsByProperty(SortType sortType, QueryProperty... property){
		return StaticQuickSorter.sort(getCommentsByProperty(property), sortType);
	}

	/**
	 * Returns Comment objects count.
	 */
	public int getCommentsCount() {
		return _getModuleASBlogData().getComments().size();
	}

	/**
	 * Returns Comment objects segment.
	 */
	public List getComments(Segment aSegment) {
		return Slicer.slice(aSegment, getComments()).getSliceData();
	}

	/**
	 * Returns Comment objects segment, where property matched.
	 */
	public List getCommentsByProperty(Segment aSegment, QueryProperty... property) {
		int pLimit = aSegment.getElementsPerSlice();
		int pOffset = aSegment.getSliceNumber() * aSegment.getElementsPerSlice() - aSegment.getElementsPerSlice();
		List ret = new ArrayList();
		List src = getComments();
		for (Comment comment : src) {;
			boolean mayPass = true;
			for (QueryProperty qp : property) {;
				mayPass = mayPass && qp.doesMatch(comment.getPropertyValue(qp.getName()));
			}
			if (mayPass)
				ret.add(comment);
			if (ret.size() > pOffset + pLimit)
				break;
		}
		return Slicer.slice(aSegment, ret).getSliceData();
	}

	/**
	 * Returns Comment objects segment, where property matched, sorted.
	 */
	public List getCommentsByProperty(Segment aSegment, SortType aSortType, QueryProperty... aProperty){
		return StaticQuickSorter.sort(getCommentsByProperty(aSegment, aProperty), aSortType);
	}

	@Override
	public void fetchComment(final String id, Set addedDocuments, JSONArray data) throws ASBlogDataServiceException {
		if (id.isEmpty() || addedDocuments.contains("Comment" + id))
			return;

		try {
			final CommentDocument comment = _getModuleASBlogData().getComment(id);
			addedDocuments.add("Comment" + id);



			JSONObject dataObject = new JSONObject();
			String jsonObject = ObjectMapperUtil.getMapperInstance().writeValueAsString(comment);
			dataObject.put("object", jsonObject);
			dataObject.put("service", "ASBlogData");
			dataObject.put("document", "ASBlogData_Comment");

			data.put(dataObject);

		} catch (JsonProcessingException e) {
			throw new ASBlogDataServiceException ("Problem with fetching data for this Comment instance object:" + e.getMessage());
		} catch (JSONException e) {
			throw new ASBlogDataServiceException ("Problem with fetching data for this Comment instance in json :" + e.getMessage());
		}
	}

	private void saveTransferredComment(final JSONObject data) throws ASBlogDataServiceException  {
		try {
			String objectData = data.getString("object");
			Comment comment = ObjectMapperUtil.getMapperInstance().readValue(objectData.getBytes(Charset.forName("UTF-8")), CommentDocument.class);

			try {
				updateComment(comment);
			} catch (Exception e) {
				importComment(comment);
			}
		} catch (JSONException e) {
			throw new ASBlogDataServiceException ("Problem with getting data from json Comment instance :" + e.getMessage());
		} catch (IOException e) {
			throw new ASBlogDataServiceException ("Problem with parsing data for this Comment instance :" + e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	public List getTags(){
		List tags = new ArrayList();
		tags.addAll(_getModuleASBlogData().getTags());
		return tags;
	}

	public List getTags(SortType sortType){
		return StaticQuickSorter.sort(getTags(), sortType);
	}

	/**
	 * Returns the Tag objects with the specified ids.
	 */
	public List getTags(List ids){;
		if (ids==null || ids.size()==0)
			return new ArrayList(0);
		List all = getTags();
		List ret = new ArrayList();
		for (Tag tag : all){
			if(ids.contains(tag.getId())){
				ret.add(tag);
			}
		}
		return ret;
	}

	/**
	 * Returns the Tag objects with the specified ids, sorted by given sorttype.
	 */
	public List getTags(List ids, SortType sortType){;
		return StaticQuickSorter.sort(getTags(ids), sortType);
	}

	public void deleteTag(Tag tag){
		deleteTag(tag.getId());
		if (hasServiceListeners()){
			fireObjectDeletedEvent(tag);
		}
	}

	public void deleteTag(String id){
		ModuleASBlogData module = _getModuleASBlogData();
		Tag varValue = hasServiceListeners()?module.getTag(id):null;
		module.deleteTag(id);
		updateModule(module);
		if(varValue!=null){
			fireObjectDeletedEvent(varValue);
		}
	}

	public void deleteTags(List list){
		ModuleASBlogData module = _getModuleASBlogData();
		for (Tag tag : list){
			module.deleteTag(tag.getId());
		}
		updateModule(module);
		if (hasServiceListeners()){
			for (int t=0; t importTags(List list){
		ModuleASBlogData module = _getModuleASBlogData();
		List ret = new ArrayList();
		for (Tag tag : list){
			Tag imported = module.importTag((TagDocument)tag);
			ret.add(imported);
		}
		updateModule(module);
		if (hasServiceListeners()){
			for (Tag tag : ret)
				fireObjectImportedEvent(tag);
		}
		return ret;
	}

	public Tag createTag(Tag tag){
		ModuleASBlogData module = _getModuleASBlogData();
		module.createTag((TagDocument)tag);
		updateModule(module);
		fireObjectCreatedEvent(tag);
		return tag;
	}

	/**
	 * Creates multiple new Tag objects.
	 * Returns the created versions.
	 */
	public List createTags(List list){;
		ModuleASBlogData module = _getModuleASBlogData();
		List ret = new ArrayList();
		for (Tag tag : list){
			Tag created = module.createTag((TagDocument)tag);
			ret.add(created);
		}
		updateModule(module);
		if (hasServiceListeners()){
			for (Tag tag : ret)
				fireObjectCreatedEvent(tag);
		}
		return ret;
	}

	public Tag updateTag(Tag tag){
		Tag oldVersion = null;
		ModuleASBlogData module = _getModuleASBlogData();
		if (hasServiceListeners())
			oldVersion = module.getTag(tag.getId());
		module.updateTag((TagDocument)tag);
		updateModule(module);
		if (oldVersion != null){
			fireObjectUpdatedEvent(oldVersion, tag);
		}
		return tag;
	}

	public List updateTags(List list){
		List oldList = null;
		if (hasServiceListeners())
			oldList = new ArrayList(list.size());
		ModuleASBlogData module = _getModuleASBlogData();
		for (Tag tag : list){
			if (oldList!=null)
				oldList.add(module.getTag(tag.getId()));
			module.updateTag((TagDocument)tag);
		}
		updateModule(module);
		if (oldList!=null){
			for (int t=0; t getTagsByProperty(String propertyName, Object value){
		List allTags = getTags();
		List ret = new ArrayList();
		for (int i=0; i getTagsByProperty(String propertyName, Object value, SortType sortType){
		return StaticQuickSorter.sort(getTagsByProperty(propertyName, value), sortType);
	}
	/**
	 * Executes a query on Tags
	 */
	public QueryResult executeQueryOnTags(DocumentQuery query){
		List allTags = getTags();
		QueryResult result = new QueryResult();
		for (int i=0; i partialResult = query.match(allTags.get(i));
			result.add(partialResult);
		}
		return result;
	}

	/**
	 * Returns all Tag objects, where property matches.
	 */
	public List getTagsByProperty(QueryProperty... property){
		//first the slow version, the fast version is a todo.
		List ret = new ArrayList();
		List src = getTags();
		for ( Tag tag : src){;
			boolean mayPass = true;
			for (QueryProperty qp : property){;
				mayPass = mayPass && qp.doesMatch(tag.getPropertyValue(qp.getName()));
			}
			if (mayPass)
				ret.add(tag);
		}
		return ret;
	}

	/**
	 * Returns all Tag objects, where property matches, sorted
	 */
	public List getTagsByProperty(SortType sortType, QueryProperty... property){
		return StaticQuickSorter.sort(getTagsByProperty(property), sortType);
	}

	/**
	 * Returns Tag objects count.
	 */
	public int getTagsCount() {
		return _getModuleASBlogData().getTags().size();
	}

	/**
	 * Returns Tag objects segment.
	 */
	public List getTags(Segment aSegment) {
		return Slicer.slice(aSegment, getTags()).getSliceData();
	}

	/**
	 * Returns Tag objects segment, where property matched.
	 */
	public List getTagsByProperty(Segment aSegment, QueryProperty... property) {
		int pLimit = aSegment.getElementsPerSlice();
		int pOffset = aSegment.getSliceNumber() * aSegment.getElementsPerSlice() - aSegment.getElementsPerSlice();
		List ret = new ArrayList();
		List src = getTags();
		for (Tag tag : src) {;
			boolean mayPass = true;
			for (QueryProperty qp : property) {;
				mayPass = mayPass && qp.doesMatch(tag.getPropertyValue(qp.getName()));
			}
			if (mayPass)
				ret.add(tag);
			if (ret.size() > pOffset + pLimit)
				break;
		}
		return Slicer.slice(aSegment, ret).getSliceData();
	}

	/**
	 * Returns Tag objects segment, where property matched, sorted.
	 */
	public List getTagsByProperty(Segment aSegment, SortType aSortType, QueryProperty... aProperty){
		return StaticQuickSorter.sort(getTagsByProperty(aSegment, aProperty), aSortType);
	}

	@Override
	public void fetchTag(final String id, Set addedDocuments, JSONArray data) throws ASBlogDataServiceException {
		if (id.isEmpty() || addedDocuments.contains("Tag" + id))
			return;

		try {
			final TagDocument tag = _getModuleASBlogData().getTag(id);
			addedDocuments.add("Tag" + id);



			JSONObject dataObject = new JSONObject();
			String jsonObject = ObjectMapperUtil.getMapperInstance().writeValueAsString(tag);
			dataObject.put("object", jsonObject);
			dataObject.put("service", "ASBlogData");
			dataObject.put("document", "ASBlogData_Tag");

			data.put(dataObject);

		} catch (JsonProcessingException e) {
			throw new ASBlogDataServiceException ("Problem with fetching data for this Tag instance object:" + e.getMessage());
		} catch (JSONException e) {
			throw new ASBlogDataServiceException ("Problem with fetching data for this Tag instance in json :" + e.getMessage());
		}
	}

	private void saveTransferredTag(final JSONObject data) throws ASBlogDataServiceException  {
		try {
			String objectData = data.getString("object");
			Tag tag = ObjectMapperUtil.getMapperInstance().readValue(objectData.getBytes(Charset.forName("UTF-8")), TagDocument.class);

			try {
				updateTag(tag);
			} catch (Exception e) {
				importTag(tag);
			}
		} catch (JSONException e) {
			throw new ASBlogDataServiceException ("Problem with getting data from json Tag instance :" + e.getMessage());
		} catch (IOException e) {
			throw new ASBlogDataServiceException ("Problem with parsing data for this Tag instance :" + e.getMessage());
		}
	}

	public void executeParsingForDocument (final DocumentName documentName, final JSONObject data) throws ASBlogDataServiceException {
		switch(documentName) {
			case DOCUMENT_ASBLOGDATA_POST:
				saveTransferredPost(data);
				break;
			case DOCUMENT_ASBLOGDATA_COMMENT:
				saveTransferredComment(data);
				break;
			case DOCUMENT_ASBLOGDATA_TAG:
				saveTransferredTag(data);
				break;
			default:
				log.info("There is no correct document: " + documentName + "in this service");
				throw new ASBlogDataServiceException("No such document");
		}
	}

	/**
	 * Executes a query on all data objects (documents, vo) which are part of this module and managed by this service
	 */
	public QueryResult executeQueryOnAllObjects(DocumentQuery query){
		QueryResult ret = new QueryResult();
		ret.add(executeQueryOnPosts(query).getEntries());
		ret.add(executeQueryOnComments(query).getEntries());
		ret.add(executeQueryOnTags(query).getEntries());
		return ret;
	} //executeQueryOnAllObjects


	public XMLNode exportPostsToXML(){;
		XMLNode ret = new XMLNode("Posts");
		List list = getPosts();
		ret.addAttribute(new XMLAttribute("count", list.size()));
		for (Post object : list)
			ret.addChildNode(PostXMLHelper.toXML(object));
		return ret;
	}

	public XMLNode exportPostsToXML(List list){;
		XMLNode ret = new XMLNode("Posts");
		ret.addAttribute(new XMLAttribute("count", list.size()));
		for (Post object : list)
			ret.addChildNode(PostXMLHelper.toXML(object));
		return ret;
	}

	public XMLNode exportCommentsToXML(){;
		XMLNode ret = new XMLNode("Comments");
		List list = getComments();
		ret.addAttribute(new XMLAttribute("count", list.size()));
		for (Comment object : list)
			ret.addChildNode(CommentXMLHelper.toXML(object));
		return ret;
	}

	public XMLNode exportCommentsToXML(List list){;
		XMLNode ret = new XMLNode("Comments");
		ret.addAttribute(new XMLAttribute("count", list.size()));
		for (Comment object : list)
			ret.addChildNode(CommentXMLHelper.toXML(object));
		return ret;
	}

	public XMLNode exportTagsToXML(){;
		XMLNode ret = new XMLNode("Tags");
		List list = getTags();
		ret.addAttribute(new XMLAttribute("count", list.size()));
		for (Tag object : list)
			ret.addChildNode(TagXMLHelper.toXML(object));
		return ret;
	}

	public XMLNode exportTagsToXML(List list){;
		XMLNode ret = new XMLNode("Tags");
		ret.addAttribute(new XMLAttribute("count", list.size()));
		for (Tag object : list)
			ret.addChildNode(TagXMLHelper.toXML(object));
		return ret;
	}

	public XMLNode exportToXML(){
		XMLNode ret = new XMLNode("ASBlogData");

		ret.addChildNode(exportPostsToXML());
		ret.addChildNode(exportCommentsToXML());
		ret.addChildNode(exportTagsToXML());

		return ret;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy