FileBackedBlogCategory.java [plain text]
package org.blojsom.blog;
import org.blojsom.util.BlojsomUtils;
import org.blojsom.util.BlojsomProperties;
import org.blojsom.util.BlojsomConstants;
import org.blojsom.BlojsomException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.List;
import java.util.ArrayList;
public class FileBackedBlogCategory extends BlogCategory {
private Log _logger = LogFactory.getLog(FileBackedBlogCategory.class);
protected BlogUser _blogUser;
public FileBackedBlogCategory() {
super();
}
public FileBackedBlogCategory(String category, String categoryURL) {
super(category, categoryURL);
}
protected void loadMetaData(String blogHome, String[] propertiesExtensions) {
File blog = new File(blogHome + BlojsomUtils.removeInitialSlash(_category));
File[] categoryPropertyFiles = blog.listFiles(BlojsomUtils.getExtensionsFilter(propertiesExtensions));
if ((categoryPropertyFiles != null) && (categoryPropertyFiles.length > 0)) {
Properties dirProps = new BlojsomProperties();
for (int i = 0; i < categoryPropertyFiles.length; i++) {
try {
FileInputStream _fis = new FileInputStream(categoryPropertyFiles[i]);
dirProps.load(_fis);
_fis.close();
} catch (IOException ex) {
_logger.warn("Failed loading properties from: " + categoryPropertyFiles[i].toString());
continue;
}
}
setMetaData(dirProps);
}
}
public void load(Blog blog) throws BlojsomException {
}
public void save(Blog blog) throws BlojsomException {
}
public void delete(Blog blog) throws BlojsomException {
}
public void load(BlogUser blogUser) throws BlojsomException {
_blogUser = blogUser;
Blog blog = blogUser.getBlog();
loadMetaData(blog.getBlogHome(), blog.getBlogPropertiesExtensions());
}
public void save(BlogUser blogUser) throws BlojsomException {
_blogUser = blogUser;
Blog blog = blogUser.getBlog();
File blogCategory = new File(blog.getBlogHome() + BlojsomUtils.removeInitialSlash(_category));
if (!blogCategory.exists()) {
if (!blogCategory.mkdirs()) {
_logger.error("Could not create new blog category at: " + blogCategory.toString());
return;
}
}
String propertiesExtension = blog.getBlogPropertiesExtensions()[0];
File categoryMetaDataFile = new File(blogCategory, "blojsom" + propertiesExtension);
Properties categoryMetaData = BlojsomUtils.mapToProperties(_metadata, BlojsomConstants.UTF8);
try {
FileOutputStream fos = new FileOutputStream(categoryMetaDataFile);
categoryMetaData.store(fos, null);
fos.close();
} catch (IOException e) {
_logger.error(e);
throw new BlojsomException("Unable to save blog category", e);
}
_logger.debug("Saved blog category: " + blogCategory.toString());
}
public void delete(BlogUser blogUser) throws BlojsomException {
_blogUser = blogUser;
Blog blog = blogUser.getBlog();
File blogCategory = new File(blog.getBlogHome() + BlojsomUtils.removeInitialSlash(_category));
if (blogCategory.equals(blog.getBlogHome())) {
if (!BlojsomUtils.deleteDirectory(blogCategory, false)) {
throw new BlojsomException("Unable to delete blog category directory: " + _category);
}
} else {
if (!BlojsomUtils.deleteDirectory(blogCategory)) {
throw new BlojsomException("Unable to delete blog category directory: " + _category);
}
}
_logger.debug("Deleted blog category: " + blogCategory.toString());
}
protected int recursiveBlogEntriesCounter(Blog blog, boolean recursive, String rootDirectory) {
File blogDirectory = new File(rootDirectory);
File[] directories;
int totalEntries = 0;
if (blog.getBlogDirectoryFilter() == null) {
directories = blogDirectory.listFiles(BlojsomUtils.getDirectoryFilter());
} else {
directories = blogDirectory.listFiles(BlojsomUtils.getDirectoryFilter(blog.getBlogDirectoryFilter()));
}
File[] entries = blogDirectory.listFiles(BlojsomUtils.getRegularExpressionFilter(blog.getBlogFileExtensions()));
if (entries == null) {
totalEntries = 0;
} else {
totalEntries = entries.length;
}
if (directories != null && recursive) {
for (int i = 0; i < directories.length; i++) {
File directory = directories[i];
return totalEntries + recursiveBlogEntriesCounter(blog, recursive, directory.toString());
}
}
return totalEntries;
}
public int countBlogEntries(Blog blog, boolean recursive) {
int totalEntries = recursiveBlogEntriesCounter(blog, recursive, blog.getBlogHome() + _category);
return totalEntries;
}
protected void recursiveCategoryBuilder(BlogUser blogUser, int blogDepth, int blogDirectoryDepth, String blogDirectory, List categoryList) {
Blog blog = blogUser.getBlog();
blogDepth++;
if (blogDirectoryDepth != BlojsomConstants.INFINITE_BLOG_DEPTH) {
if (blogDepth == blogDirectoryDepth) {
return;
}
}
File blogDir = new File(blogDirectory);
File[] directories;
if (blog.getBlogDirectoryFilter() == null) {
directories = blogDir.listFiles(BlojsomUtils.getDirectoryFilter());
} else {
directories = blogDir.listFiles(BlojsomUtils.getDirectoryFilter(blog.getBlogDirectoryFilter()));
}
String categoryKey = BlojsomUtils.getBlogCategory(blog.getBlogHome(), blogDirectory);
if (!categoryKey.endsWith("/")) {
categoryKey += "/";
}
BlogCategory blogCategory = new FileBackedBlogCategory();
blogCategory.setParentCategory(this);
blogCategory.setCategory(categoryKey);
blogCategory.setCategoryURL(blog.getBlogURL() + BlojsomUtils.removeInitialSlash(categoryKey));
try {
blogCategory.load(blogUser);
} catch (BlojsomException e) {
_logger.error(e);
}
if (blogDepth != 0) {
categoryList.add(blogCategory);
}
if (directories == null) {
return;
} else {
for (int i = 0; i < directories.length; i++) {
File directory = directories[i];
recursiveCategoryBuilder(blogUser, blogDepth, blogDirectoryDepth, directory.toString(), categoryList);
}
}
}
public List getSubcategories() {
_subcategories = new ArrayList();
recursiveCategoryBuilder(_blogUser, -1, -1, _blogUser.getBlog().getBlogHome() + BlojsomUtils.removeInitialSlash(_category), _subcategories);
return _subcategories;
}
}