|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| This report was generated with an evaluation server license. Purchase Clover or configure your license. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FileCategoryDAO | Line # 24 | 45 | 0% | 10 | 21 | 60,4% |
0.6037736
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (16) | |||
| Result | |||
|
0.6037736
|
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testComment
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testComment
|
1 PASS | |
|
0.6037736
|
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testMultipleSubCategory
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testMultipleSubCategory
|
1 PASS | |
|
0.6037736
|
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testNoExcerpt
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testNoExcerpt
|
1 PASS | |
|
0.6037736
|
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testNoExcerptNoExtendedBody
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testNoExcerptNoExtendedBody
|
1 PASS | |
|
0.6037736
|
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testMultipleSubCategory
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testMultipleSubCategory
|
1 PASS | |
|
0.6037736
|
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testImport
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testImport
|
1 PASS | |
|
0.6037736
|
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testUTF8
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testUTF8
|
1 PASS | |
|
0.6037736
|
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testImport
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testImport
|
1 PASS | |
|
0.6037736
|
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testComment
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testComment
|
1 PASS | |
|
0.6037736
|
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testPublished
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testPublished
|
1 PASS | |
|
0.6037736
|
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testNoExcerpt
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testNoExcerpt
|
1 PASS | |
|
0.6037736
|
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testUTF8
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testUTF8
|
1 PASS | |
|
0.6037736
|
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testNoExcerptNoExtendedBody
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testNoExcerptNoExtendedBody
|
1 PASS | |
|
0.6037736
|
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testPublished
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testPublished
|
1 PASS | |
|
0.16981132
|
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testNoPrimaryCategory
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testNoPrimaryCategory
|
1 PASS | |
|
0.16981132
|
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testNoPrimaryCategory
net.sourceforge.pebble.util.importer.MovableTypeImporterTest.testNoPrimaryCategory
|
1 PASS | |
| 1 | package net.sourceforge.pebble.dao.file; | |
| 2 | ||
| 3 | import net.sourceforge.pebble.dao.CategoryDAO; | |
| 4 | import net.sourceforge.pebble.dao.PersistenceException; | |
| 5 | import net.sourceforge.pebble.domain.Blog; | |
| 6 | import net.sourceforge.pebble.domain.Category; | |
| 7 | import net.sourceforge.pebble.domain.CategoryBuilder; | |
| 8 | import org.apache.commons.logging.Log; | |
| 9 | import org.apache.commons.logging.LogFactory; | |
| 10 | ||
| 11 | import javax.xml.bind.JAXBContext; | |
| 12 | import javax.xml.bind.JAXBElement; | |
| 13 | import javax.xml.bind.Marshaller; | |
| 14 | import javax.xml.bind.Unmarshaller; | |
| 15 | import java.io.File; | |
| 16 | import java.io.FileWriter; | |
| 17 | import java.util.List; | |
| 18 | ||
| 19 | /** | |
| 20 | * DAO responsible for managing the storage of category definitions. | |
| 21 | * | |
| 22 | * @author Simon Brown | |
| 23 | */ | |
| 24 | public class FileCategoryDAO implements CategoryDAO { | |
| 25 | ||
| 26 | /** the name of the file containing the category information */ | |
| 27 | private static final String CATEGORIES_FILE_NAME = "categories.xml"; | |
| 28 | ||
| 29 | /** the log used by this class */ | |
| 30 | private static Log log = LogFactory.getLog(FileCategoryDAO.class); | |
| 31 | ||
| 32 | private JAXBContext jaxbContext; | |
| 33 | ||
| 34 | /** | |
| 35 | * Default, no args constructor. | |
| 36 | */ | |
| 37 | 2454 |
public FileCategoryDAO() { |
| 38 | 2454 | try { |
| 39 | 2454 | jaxbContext = JAXBContext.newInstance(getClass().getPackage().getName()); |
| 40 | } catch (Exception e) { | |
| 41 | 0 | e.printStackTrace(); |
| 42 | } | |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * Gets the categories for a particular blog. | |
| 47 | * | |
| 48 | * @param blog the owning Blog instance | |
| 49 | * @return a Collection of Category instances | |
| 50 | * @throws PersistenceException if categories cannot be loaded | |
| 51 | */ | |
| 52 | 16 |
public Category getCategories(Blog blog) throws PersistenceException { |
| 53 | 16 | CategoryBuilder categoryBuilder = new CategoryBuilder(blog); |
| 54 | 16 | File source = new File(blog.getRoot(), CATEGORIES_FILE_NAME); |
| 55 | 16 | if (source.exists()) { |
| 56 | 0 | try { |
| 57 | 0 | Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); |
| 58 | 0 | JAXBElement<CategoriesType> controller = (JAXBElement)unmarshaller.unmarshal(source); |
| 59 | 0 | CategoriesType categoriesType = controller.getValue(); |
| 60 | ||
| 61 | 0 | for (CategoryType categoryType : categoriesType.getCategory()) { |
| 62 | 0 | Category category = new Category(categoryType.getId(), categoryType.getName()); |
| 63 | 0 | category.setBlog(blog); |
| 64 | 0 | category.setTags(categoryType.getTags()); |
| 65 | ||
| 66 | 0 | categoryBuilder.addCategory(category); |
| 67 | } | |
| 68 | } catch (Exception e) { | |
| 69 | 0 | log.error(e.getMessage(), e); |
| 70 | 0 | e.printStackTrace(); |
| 71 | 0 | throw new PersistenceException(e.getMessage()); |
| 72 | } | |
| 73 | } | |
| 74 | ||
| 75 | 16 | return categoryBuilder.getRootCategory(); |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * Adds the specified category. | |
| 80 | * | |
| 81 | * @param category the Category instance to be added | |
| 82 | * @param blog the owning blog | |
| 83 | * @throws PersistenceException if something goes wrong storing the category | |
| 84 | */ | |
| 85 | 16 |
public void addCategory(Category category, Blog blog) throws PersistenceException { |
| 86 | 16 | store(blog); |
| 87 | } | |
| 88 | ||
| 89 | /** | |
| 90 | * Updates the specified category. | |
| 91 | * | |
| 92 | * @param updatedCategory the Category instance to be updated | |
| 93 | * @param blog the owning blog | |
| 94 | * @throws PersistenceException if something goes wrong storing the category | |
| 95 | */ | |
| 96 | 0 |
public void updateCategory(Category updatedCategory, Blog blog) throws PersistenceException { |
| 97 | 0 | store(blog); |
| 98 | } | |
| 99 | ||
| 100 | /** | |
| 101 | * Removes the specified category. | |
| 102 | * | |
| 103 | * @param category the Category instance to be removed | |
| 104 | * @param blog the owning blog | |
| 105 | * @throws PersistenceException if something goes wrong removing the category | |
| 106 | */ | |
| 107 | 0 |
public void deleteCategory(Category category, Blog blog) throws PersistenceException { |
| 108 | 0 | store(blog); |
| 109 | } | |
| 110 | ||
| 111 | /** | |
| 112 | * Helper method to store all categories for a given blog. | |
| 113 | * | |
| 114 | * @param blog the blog to which the categories belong | |
| 115 | * @throws PersistenceException if the categories cannnot be stored | |
| 116 | */ | |
| 117 | 16 |
private void store(Blog blog) throws PersistenceException { |
| 118 | 16 | List<Category> categories = blog.getCategories(); |
| 119 | 16 | File destination = new File(blog.getRoot(), CATEGORIES_FILE_NAME); |
| 120 | 16 | try { |
| 121 | 16 | Marshaller marshaller = jaxbContext.createMarshaller(); |
| 122 | 16 | CategoriesType categoriesType = new CategoriesType(); |
| 123 | ||
| 124 | 16 | for (Category category : categories) { |
| 125 | 18 | CategoryType categoryType = new CategoryType(); |
| 126 | 18 | categoryType.setId(category.getId()); |
| 127 | 18 | categoryType.setName(category.getName()); |
| 128 | 18 | categoryType.setTags(category.getTags()); |
| 129 | 18 | categoriesType.getCategory().add(categoryType); |
| 130 | } | |
| 131 | ||
| 132 | 16 | log.debug("Saving to " + destination.getAbsolutePath()); |
| 133 | 16 | ObjectFactory objectFactory = new ObjectFactory(); |
| 134 | 16 | JAXBElement jaxbElement = objectFactory.createCategories(categoriesType); |
| 135 | ||
| 136 | 16 | marshaller.setProperty("jaxb.formatted.output", true); |
| 137 | 16 | marshaller.setProperty("jaxb.encoding", blog.getCharacterEncoding()); |
| 138 | 16 | FileWriter writer = new FileWriter(destination); |
| 139 | 16 | marshaller.marshal(jaxbElement, writer); |
| 140 | 16 | writer.flush(); |
| 141 | 16 | writer.close(); |
| 142 | } catch (Exception e) { | |
| 143 | 0 | log.error(e.getMessage(), e); |
| 144 | 0 | e.printStackTrace(); |
| 145 | 0 | throw new PersistenceException(e.getMessage()); |
| 146 | } | |
| 147 | } | |
| 148 | ||
| 149 | } | |
|
||||||||||||