| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki.store; |
| 21 |
|
|
| 22 |
|
import java.util.Iterator; |
| 23 |
|
import java.util.List; |
| 24 |
|
|
| 25 |
|
import javax.inject.Named; |
| 26 |
|
import javax.inject.Singleton; |
| 27 |
|
|
| 28 |
|
import org.hibernate.Query; |
| 29 |
|
import org.hibernate.Session; |
| 30 |
|
import org.slf4j.Logger; |
| 31 |
|
import org.slf4j.LoggerFactory; |
| 32 |
|
import org.xwiki.component.annotation.Component; |
| 33 |
|
|
| 34 |
|
import com.xpn.xwiki.XWiki; |
| 35 |
|
import com.xpn.xwiki.XWikiContext; |
| 36 |
|
import com.xpn.xwiki.XWikiException; |
| 37 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
| 38 |
|
import com.xpn.xwiki.doc.XWikiAttachmentContent; |
| 39 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 40 |
|
|
| 41 |
|
@Component |
| 42 |
|
@Named("hibernate") |
| 43 |
|
@Singleton |
| |
|
| 50.6% |
Uncovered Elements: 86 (174) |
Complexity: 48 |
Complexity Density: 0.44 |
|
| 44 |
|
public class XWikiHibernateAttachmentStore extends XWikiHibernateBaseStore implements XWikiAttachmentStoreInterface |
| 45 |
|
{ |
| 46 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(XWikiHibernateAttachmentStore.class); |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
@param |
| 53 |
|
@param |
| 54 |
|
@deprecated |
| 55 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 56 |
0 |
@Deprecated... |
| 57 |
|
public XWikiHibernateAttachmentStore(XWiki xwiki, XWikiContext context) |
| 58 |
|
{ |
| 59 |
0 |
super(xwiki, context); |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
@see |
| 64 |
|
@deprecated |
| 65 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 66 |
0 |
@Deprecated... |
| 67 |
|
public XWikiHibernateAttachmentStore(XWikiContext context) |
| 68 |
|
{ |
| 69 |
0 |
this(context.getWiki(), context); |
| 70 |
|
} |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
@param |
| 76 |
|
@deprecated |
| 77 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 78 |
0 |
@Deprecated... |
| 79 |
|
public XWikiHibernateAttachmentStore(String hibpath) |
| 80 |
|
{ |
| 81 |
0 |
super(hibpath); |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 87 |
87 |
public XWikiHibernateAttachmentStore()... |
| 88 |
|
{ |
| 89 |
|
} |
| 90 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 91 |
0 |
@Override... |
| 92 |
|
public void saveAttachmentContent(XWikiAttachment attachment, XWikiContext context, boolean bTransaction) |
| 93 |
|
throws XWikiException |
| 94 |
|
{ |
| 95 |
0 |
saveAttachmentContent(attachment, true, context, bTransaction); |
| 96 |
|
} |
| 97 |
|
|
| |
|
| 69.4% |
Uncovered Elements: 15 (49) |
Complexity: 12 |
Complexity Density: 0.39 |
|
| 98 |
423 |
@Override... |
| 99 |
|
public void saveAttachmentContent(XWikiAttachment attachment, boolean parentUpdate, XWikiContext inputxcontext, |
| 100 |
|
boolean bTransaction) throws XWikiException |
| 101 |
|
{ |
| 102 |
423 |
XWikiContext context = getXWikiContext(inputxcontext); |
| 103 |
|
|
| 104 |
423 |
XWikiAttachmentContent content = attachment.getAttachment_content(); |
| 105 |
|
|
| 106 |
|
|
| 107 |
423 |
if (content != null) { |
| 108 |
423 |
String currentWiki = context.getWikiId(); |
| 109 |
|
|
| 110 |
423 |
try { |
| 111 |
|
|
| 112 |
423 |
String attachmentWiki = |
| 113 |
423 |
(attachment.getReference() == null) ? null : attachment.getReference().getDocumentReference() |
| 114 |
|
.getWikiReference().getName(); |
| 115 |
423 |
if (attachmentWiki != null) { |
| 116 |
423 |
context.setWikiId(attachmentWiki); |
| 117 |
|
} |
| 118 |
|
|
| 119 |
423 |
if (bTransaction) { |
| 120 |
0 |
checkHibernate(context); |
| 121 |
0 |
bTransaction = beginTransaction(context); |
| 122 |
|
} |
| 123 |
423 |
Session session = getSession(context); |
| 124 |
|
|
| 125 |
423 |
Query query = |
| 126 |
|
session.createQuery("select attach.id from XWikiAttachmentContent as attach where attach.id = :id"); |
| 127 |
423 |
query.setLong("id", content.getId()); |
| 128 |
423 |
if (query.uniqueResult() == null) { |
| 129 |
416 |
session.save(content); |
| 130 |
|
} else { |
| 131 |
7 |
session.update(content); |
| 132 |
|
} |
| 133 |
|
|
| 134 |
423 |
if (attachment.getAttachment_archive() == null) { |
| 135 |
365 |
attachment.loadArchive(context); |
| 136 |
|
} |
| 137 |
|
|
| 138 |
423 |
context.getWiki().getAttachmentVersioningStore() |
| 139 |
|
.saveArchive(attachment.getAttachment_archive(), context, false); |
| 140 |
|
|
| 141 |
423 |
if (parentUpdate) { |
| 142 |
0 |
context.getWiki().getStore().saveXWikiDoc(attachment.getDoc(), context, true); |
| 143 |
|
} |
| 144 |
|
|
| 145 |
423 |
if (bTransaction) { |
| 146 |
0 |
endTransaction(context, true); |
| 147 |
|
} |
| 148 |
|
} catch (Exception e) { |
| 149 |
0 |
Object[] args = { attachment.getFilename(), attachment.getDoc().getFullName() }; |
| 150 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
| 151 |
|
XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_ATTACHMENT, |
| 152 |
|
"Exception while saving attachment {0} of document {1}", e, args); |
| 153 |
|
} finally { |
| 154 |
423 |
try { |
| 155 |
423 |
if (bTransaction) { |
| 156 |
0 |
endTransaction(context, false); |
| 157 |
|
} |
| 158 |
|
} catch (Exception e) { |
| 159 |
|
} |
| 160 |
|
|
| 161 |
|
|
| 162 |
423 |
context.setWikiId(currentWiki); |
| 163 |
|
} |
| 164 |
|
} else { |
| 165 |
0 |
LOGGER.warn("Failed to save the Attachment content for [{}] at [{}] since no content could be found!", |
| 166 |
|
attachment.getFilename(), attachment.getDoc()); |
| 167 |
|
} |
| 168 |
|
} |
| 169 |
|
|
| |
|
| 0% |
Uncovered Elements: 27 (27) |
Complexity: 8 |
Complexity Density: 0.47 |
|
| 170 |
0 |
@Override... |
| 171 |
|
public void saveAttachmentsContent(List<XWikiAttachment> attachments, XWikiDocument doc, boolean bParentUpdate, |
| 172 |
|
XWikiContext inputxcontext, boolean bTransaction) throws XWikiException |
| 173 |
|
{ |
| 174 |
0 |
XWikiContext context = getXWikiContext(inputxcontext); |
| 175 |
|
|
| 176 |
0 |
if (attachments == null) { |
| 177 |
0 |
return; |
| 178 |
|
} |
| 179 |
0 |
try { |
| 180 |
0 |
if (bTransaction) { |
| 181 |
0 |
checkHibernate(context); |
| 182 |
0 |
bTransaction = beginTransaction(context); |
| 183 |
|
} |
| 184 |
0 |
Iterator<XWikiAttachment> it = attachments.iterator(); |
| 185 |
0 |
while (it.hasNext()) { |
| 186 |
0 |
XWikiAttachment att = it.next(); |
| 187 |
0 |
saveAttachmentContent(att, false, context, false); |
| 188 |
|
} |
| 189 |
0 |
if (bParentUpdate) { |
| 190 |
0 |
context.getWiki().getStore().saveXWikiDoc(doc, context, false); |
| 191 |
|
} |
| 192 |
|
} catch (Exception e) { |
| 193 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
| 194 |
|
XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_ATTACHMENT, "Exception while saving attachments", e); |
| 195 |
|
} finally { |
| 196 |
0 |
try { |
| 197 |
0 |
if (bTransaction) { |
| 198 |
0 |
endTransaction(context, false); |
| 199 |
|
} |
| 200 |
|
} catch (Exception e) { |
| 201 |
|
} |
| 202 |
|
} |
| 203 |
|
|
| 204 |
|
} |
| 205 |
|
|
| |
|
| 78.1% |
Uncovered Elements: 7 (32) |
Complexity: 8 |
Complexity Density: 0.36 |
|
| 206 |
281 |
@Override... |
| 207 |
|
public void loadAttachmentContent(XWikiAttachment attachment, XWikiContext inputxcontext, boolean bTransaction) |
| 208 |
|
throws XWikiException |
| 209 |
|
{ |
| 210 |
281 |
XWikiContext context = getXWikiContext(inputxcontext); |
| 211 |
|
|
| 212 |
279 |
String currentWiki = context.getWikiId(); |
| 213 |
|
|
| 214 |
280 |
try { |
| 215 |
|
|
| 216 |
281 |
String attachmentWiki = |
| 217 |
281 |
(attachment.getReference() == null) ? null : attachment.getReference().getDocumentReference() |
| 218 |
|
.getWikiReference().getName(); |
| 219 |
281 |
if (attachmentWiki != null) { |
| 220 |
280 |
context.setWikiId(attachmentWiki); |
| 221 |
|
} |
| 222 |
|
|
| 223 |
281 |
if (bTransaction) { |
| 224 |
281 |
checkHibernate(context); |
| 225 |
278 |
bTransaction = beginTransaction(false, context); |
| 226 |
|
} |
| 227 |
279 |
Session session = getSession(context); |
| 228 |
|
|
| 229 |
281 |
XWikiAttachmentContent content = new XWikiAttachmentContent(attachment); |
| 230 |
281 |
session.load(content, new Long(content.getId())); |
| 231 |
|
|
| 232 |
|
|
| 233 |
281 |
content.setContentDirty(false); |
| 234 |
|
|
| 235 |
281 |
attachment.setAttachment_content(content); |
| 236 |
|
|
| 237 |
281 |
if (bTransaction) { |
| 238 |
281 |
endTransaction(context, false, false); |
| 239 |
|
} |
| 240 |
|
} catch (Exception e) { |
| 241 |
0 |
Object[] args = { attachment.getFilename(), attachment.getDoc().getFullName() }; |
| 242 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
| 243 |
|
XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_ATTACHMENT, |
| 244 |
|
"Exception while loading attachment {0} of document {1}", e, args); |
| 245 |
|
} finally { |
| 246 |
281 |
try { |
| 247 |
281 |
if (bTransaction) { |
| 248 |
280 |
endTransaction(context, false, false); |
| 249 |
|
} |
| 250 |
|
} catch (Exception e) { |
| 251 |
|
} |
| 252 |
|
|
| 253 |
|
|
| 254 |
281 |
context.setWikiId(currentWiki); |
| 255 |
|
} |
| 256 |
|
} |
| 257 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 258 |
0 |
@Override... |
| 259 |
|
public void deleteXWikiAttachment(XWikiAttachment attachment, XWikiContext context, boolean bTransaction) |
| 260 |
|
throws XWikiException |
| 261 |
|
{ |
| 262 |
0 |
deleteXWikiAttachment(attachment, true, context, bTransaction); |
| 263 |
|
} |
| 264 |
|
|
| |
|
| 49% |
Uncovered Elements: 26 (51) |
Complexity: 14 |
Complexity Density: 0.4 |
|
| 265 |
22 |
@Override... |
| 266 |
|
public void deleteXWikiAttachment(XWikiAttachment attachment, boolean parentUpdate, XWikiContext inputxcontext, |
| 267 |
|
boolean bTransaction) throws XWikiException |
| 268 |
|
{ |
| 269 |
22 |
XWikiContext context = getXWikiContext(inputxcontext); |
| 270 |
|
|
| 271 |
22 |
String currentWiki = context.getWikiId(); |
| 272 |
|
|
| 273 |
22 |
try { |
| 274 |
|
|
| 275 |
22 |
String attachmentWiki = |
| 276 |
22 |
(attachment.getReference() == null) ? null : attachment.getReference().getDocumentReference() |
| 277 |
|
.getWikiReference().getName(); |
| 278 |
22 |
if (attachmentWiki != null) { |
| 279 |
22 |
context.setWikiId(attachmentWiki); |
| 280 |
|
} |
| 281 |
|
|
| 282 |
22 |
if (bTransaction) { |
| 283 |
0 |
checkHibernate(context); |
| 284 |
0 |
bTransaction = beginTransaction(context); |
| 285 |
|
} |
| 286 |
|
|
| 287 |
22 |
Session session = getSession(context); |
| 288 |
|
|
| 289 |
|
|
| 290 |
22 |
try { |
| 291 |
22 |
session.delete(new XWikiAttachmentContent(attachment)); |
| 292 |
|
} catch (Exception e) { |
| 293 |
0 |
LOGGER.warn("Error deleting attachment content [{}] of document [{}]", attachment.getFilename(), |
| 294 |
|
attachment.getDoc().getDocumentReference()); |
| 295 |
|
} |
| 296 |
|
|
| 297 |
22 |
context.getWiki().getAttachmentVersioningStore().deleteArchive(attachment, context, false); |
| 298 |
|
|
| 299 |
22 |
try { |
| 300 |
22 |
session.delete(attachment); |
| 301 |
|
} catch (Exception e) { |
| 302 |
0 |
LOGGER.warn("Error deleting attachment meta data [{}] of document [{}]", attachment.getFilename(), |
| 303 |
|
attachment.getDoc().getDocumentReference()); |
| 304 |
|
} |
| 305 |
|
|
| 306 |
22 |
try { |
| 307 |
22 |
if (parentUpdate) { |
| 308 |
0 |
List<XWikiAttachment> list = attachment.getDoc().getAttachmentList(); |
| 309 |
0 |
for (int i = 0; i < list.size(); i++) { |
| 310 |
0 |
XWikiAttachment attach = list.get(i); |
| 311 |
0 |
if (attachment.getFilename().equals(attach.getFilename())) { |
| 312 |
0 |
list.remove(i); |
| 313 |
0 |
break; |
| 314 |
|
} |
| 315 |
|
} |
| 316 |
0 |
context.getWiki().getStore().saveXWikiDoc(attachment.getDoc(), context, false); |
| 317 |
|
} |
| 318 |
|
} catch (Exception e) { |
| 319 |
0 |
LOGGER.warn("Error updating document when deleting attachment [{}] of document [{}]", |
| 320 |
|
attachment.getFilename(), attachment.getDoc().getDocumentReference()); |
| 321 |
|
} |
| 322 |
|
|
| 323 |
22 |
if (bTransaction) { |
| 324 |
0 |
endTransaction(context, true); |
| 325 |
|
} |
| 326 |
|
} catch (Exception e) { |
| 327 |
0 |
Object[] args = { attachment.getFilename(), attachment.getDoc().getFullName() }; |
| 328 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
| 329 |
|
XWikiException.ERROR_XWIKI_STORE_HIBERNATE_DELETING_ATTACHMENT, |
| 330 |
|
"Exception while deleting attachment {0} of document {1}", e, args); |
| 331 |
|
} finally { |
| 332 |
22 |
try { |
| 333 |
22 |
if (bTransaction) { |
| 334 |
0 |
endTransaction(context, false); |
| 335 |
|
} |
| 336 |
|
} catch (Exception e) { |
| 337 |
|
} |
| 338 |
|
|
| 339 |
|
|
| 340 |
22 |
context.setWikiId(currentWiki); |
| 341 |
|
} |
| 342 |
|
} |
| 343 |
|
} |