| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
package net.sourceforge.pebble.dao.file; |
| 34 | |
|
| 35 | |
import net.sourceforge.pebble.dao.CategoryDAO; |
| 36 | |
import net.sourceforge.pebble.dao.PersistenceException; |
| 37 | |
import net.sourceforge.pebble.domain.Blog; |
| 38 | |
import net.sourceforge.pebble.domain.Category; |
| 39 | |
import net.sourceforge.pebble.domain.CategoryBuilder; |
| 40 | |
import org.apache.commons.logging.Log; |
| 41 | |
import org.apache.commons.logging.LogFactory; |
| 42 | |
|
| 43 | |
import javax.xml.bind.JAXBContext; |
| 44 | |
import javax.xml.bind.JAXBElement; |
| 45 | |
import javax.xml.bind.Marshaller; |
| 46 | |
import javax.xml.bind.Unmarshaller; |
| 47 | |
import java.io.File; |
| 48 | |
import java.io.FileWriter; |
| 49 | |
import java.util.List; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public class FileCategoryDAO implements CategoryDAO { |
| 57 | |
|
| 58 | |
|
| 59 | |
private static final String CATEGORIES_FILE_NAME = "categories.xml"; |
| 60 | |
|
| 61 | |
|
| 62 | 4 | private static Log log = LogFactory.getLog(FileCategoryDAO.class); |
| 63 | |
|
| 64 | |
private JAXBContext jaxbContext; |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | 5020 | public FileCategoryDAO() { |
| 70 | |
try { |
| 71 | 5020 | jaxbContext = JAXBContext.newInstance(getClass().getPackage().getName()); |
| 72 | 0 | } catch (Exception e) { |
| 73 | 0 | e.printStackTrace(); |
| 74 | 5020 | } |
| 75 | 5020 | } |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
public Category getCategories(Blog blog) throws PersistenceException { |
| 85 | 32 | CategoryBuilder categoryBuilder = new CategoryBuilder(blog); |
| 86 | 32 | File source = new File(blog.getRoot(), CATEGORIES_FILE_NAME); |
| 87 | 32 | if (source.exists()) { |
| 88 | |
try { |
| 89 | 0 | Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); |
| 90 | 0 | JAXBElement<CategoriesType> controller = (JAXBElement)unmarshaller.unmarshal(source); |
| 91 | 0 | CategoriesType categoriesType = controller.getValue(); |
| 92 | |
|
| 93 | 0 | for (CategoryType categoryType : categoriesType.getCategory()) { |
| 94 | 0 | Category category = new Category(categoryType.getId(), categoryType.getName()); |
| 95 | 0 | category.setBlog(blog); |
| 96 | 0 | category.setTags(categoryType.getTags()); |
| 97 | |
|
| 98 | 0 | categoryBuilder.addCategory(category); |
| 99 | 0 | } |
| 100 | 0 | } catch (Exception e) { |
| 101 | 0 | log.error(e.getMessage(), e); |
| 102 | 0 | e.printStackTrace(); |
| 103 | 0 | throw new PersistenceException(e.getMessage()); |
| 104 | 0 | } |
| 105 | |
} |
| 106 | |
|
| 107 | 32 | return categoryBuilder.getRootCategory(); |
| 108 | |
} |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
public void addCategory(Category category, Blog blog) throws PersistenceException { |
| 118 | 32 | store(blog); |
| 119 | 32 | } |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
public void updateCategory(Category updatedCategory, Blog blog) throws PersistenceException { |
| 129 | 0 | store(blog); |
| 130 | 0 | } |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
public void deleteCategory(Category category, Blog blog) throws PersistenceException { |
| 140 | 0 | store(blog); |
| 141 | 0 | } |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
private void store(Blog blog) throws PersistenceException { |
| 150 | 32 | List<Category> categories = blog.getCategories(); |
| 151 | 32 | File destination = new File(blog.getRoot(), CATEGORIES_FILE_NAME); |
| 152 | |
try { |
| 153 | 32 | Marshaller marshaller = jaxbContext.createMarshaller(); |
| 154 | 32 | CategoriesType categoriesType = new CategoriesType(); |
| 155 | |
|
| 156 | 32 | for (Category category : categories) { |
| 157 | 36 | CategoryType categoryType = new CategoryType(); |
| 158 | 36 | categoryType.setId(category.getId()); |
| 159 | 36 | categoryType.setName(category.getName()); |
| 160 | 36 | categoryType.setTags(category.getTags()); |
| 161 | 36 | categoriesType.getCategory().add(categoryType); |
| 162 | 36 | } |
| 163 | |
|
| 164 | 32 | log.debug("Saving to " + destination.getAbsolutePath()); |
| 165 | 32 | ObjectFactory objectFactory = new ObjectFactory(); |
| 166 | 32 | JAXBElement jaxbElement = objectFactory.createCategories(categoriesType); |
| 167 | |
|
| 168 | 32 | marshaller.setProperty("jaxb.formatted.output", true); |
| 169 | 32 | marshaller.setProperty("jaxb.encoding", blog.getCharacterEncoding()); |
| 170 | 32 | FileWriter writer = new FileWriter(destination); |
| 171 | 32 | marshaller.marshal(jaxbElement, writer); |
| 172 | 32 | writer.flush(); |
| 173 | 32 | writer.close(); |
| 174 | 0 | } catch (Exception e) { |
| 175 | 0 | log.error(e.getMessage(), e); |
| 176 | 0 | e.printStackTrace(); |
| 177 | 0 | throw new PersistenceException(e.getMessage()); |
| 178 | 32 | } |
| 179 | 32 | } |
| 180 | |
|
| 181 | |
} |