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 |
|
|
21 |
|
|
22 |
|
@author |
23 |
|
|
|
|
| 60,4% |
Uncovered Elements: 21 (53) |
Complexity: 10 |
Complexity Density: 0,22 |
|
24 |
|
public class FileCategoryDAO implements CategoryDAO { |
25 |
|
|
26 |
|
|
27 |
|
private static final String CATEGORIES_FILE_NAME = "categories.xml"; |
28 |
|
|
29 |
|
|
30 |
|
private static Log log = LogFactory.getLog(FileCategoryDAO.class); |
31 |
|
|
32 |
|
private JAXBContext jaxbContext; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
|
|
| 66,7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0,67 |
|
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 |
|
|
47 |
|
|
48 |
|
@param |
49 |
|
@return |
50 |
|
@throws |
51 |
|
|
|
|
| 27,8% |
Uncovered Elements: 13 (18) |
Complexity: 3 |
Complexity Density: 0,19 |
|
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 |
|
|
80 |
|
|
81 |
|
@param |
82 |
|
@param |
83 |
|
@throws |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
85 |
16
|
public void addCategory(Category category, Blog blog) throws PersistenceException {... |
86 |
16
|
store(blog); |
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
@param |
93 |
|
@param |
94 |
|
@throws |
95 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
96 |
0
|
public void updateCategory(Category updatedCategory, Blog blog) throws PersistenceException {... |
97 |
0
|
store(blog); |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
@param |
104 |
|
@param |
105 |
|
@throws |
106 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
107 |
0
|
public void deleteCategory(Category category, Blog blog) throws PersistenceException {... |
108 |
0
|
store(blog); |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
@param |
115 |
|
@throws |
116 |
|
|
|
|
| 87% |
Uncovered Elements: 3 (23) |
Complexity: 2 |
Complexity Density: 0,09 |
|
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 |
|
} |