use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class R910100XWIKI14871DataMigration method storeDeletedAttachment.
private void storeDeletedAttachment(File directory, long id, Session session) throws ParserConfigurationException, SAXException, IOException {
this.logger.info("Storing attachment metadata [{}] in the database", directory);
// Find document reference
File documentDirectory = directory.getParentFile().getParentFile().getParentFile();
DocumentReference documentReference = getDocumentReference(documentDirectory);
if (getXWikiContext().getWikiReference().equals(documentReference.getWikiReference())) {
// Parse ~DELETED_ATTACH_METADATA.xml
File file = new File(directory, "~DELETED_ATTACH_METADATA.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(file);
String filename = getElementText(doc, "filename", null);
String deleter = getElementText(doc, "deleter", null);
Date deleteDate = new Date(Long.valueOf(getElementText(doc, "datedeleted", null)));
long docId = new XWikiDocument(documentReference).getId();
// We need to make sure the deleted attachment is not already in the database with a different id (left
// there by the attachment porter script for example)
Query selectQuery = session.createQuery("SELECT id FROM DeletedAttachment WHERE docId=? AND filename=? AND date=?");
selectQuery.setLong(0, docId);
selectQuery.setString(1, filename);
selectQuery.setTimestamp(2, new java.sql.Timestamp(deleteDate.getTime()));
Long databaseId = (Long) selectQuery.uniqueResult();
if (databaseId == null) {
// Try without the milliseconds since most versions of MySQL don't support them
selectQuery.setTimestamp(2, new java.sql.Timestamp(deleteDate.toInstant().getEpochSecond() * 1000));
databaseId = (Long) selectQuery.uniqueResult();
}
DeletedAttachment dbAttachment;
if (databaseId != null) {
// Update the database metadata (probably left there by the attachment porter script)
dbAttachment = new DeletedAttachment(docId, this.serializer.serialize(documentReference), filename, FileSystemStoreUtils.HINT, deleter, deleteDate, null, databaseId);
session.update(dbAttachment);
} else {
// Insert new deleted attachment metadata in the DB
dbAttachment = new DeletedAttachment(docId, this.serializer.serialize(documentReference), filename, FileSystemStoreUtils.HINT, deleter, deleteDate, null);
databaseId = (Long) session.save(dbAttachment);
}
// Refactor file storage to be based on database id instead of date
File newDirectory = new File(directory.getParentFile(), encode(dbAttachment.getFilename() + "-id" + databaseId));
FileUtils.moveDirectory(directory, newDirectory);
}
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class AbstractSxExportURLFactoryActionHandler method createURL.
@Override
public URL createURL(String spaces, String name, String queryString, String anchor, String wikiId, XWikiContext context, FilesystemExportContext exportContext) throws Exception {
// Check if the current user has the right to view the SX file. We do this since this is what would happen
// in XE when a SX action is called (check done in XWikiAction).
// Note that we cannot just open an HTTP connection to the SX action here since we wouldn't be authenticated...
// Thus we have to simulate the same behavior as the SX action...
List<String> spaceNames = this.legacySpaceResolve.resolve(spaces);
DocumentReference sxDocumentReference = new DocumentReference(wikiId, spaceNames, name);
this.authorizationManager.checkAccess(Right.VIEW, sxDocumentReference);
// Set the SX document as the current document in the XWiki Context since unfortunately the SxSource code
// uses the current document in the context instead of accepting it as a parameter...
XWikiDocument sxDocument = context.getWiki().getDocument(sxDocumentReference, context);
Map<String, Object> backup = new HashMap<>();
XWikiDocument.backupContext(backup, context);
try {
sxDocument.setAsContextDoc(context);
return processSx(spaceNames, name, queryString, context, exportContext);
} finally {
XWikiDocument.restoreContext(backup, context);
}
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class DefaultWikiUserManager method removeMembers.
@Override
public void removeMembers(Collection<String> userIds, String wikiId) throws WikiUserManagerException {
// Get the group document
XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
// Get the member objects
List<BaseObject> objects = groupDoc.getXObjects(GROUPCLASS_REFERENCE);
if (objects != null) {
// Get the member objects to remove
List<BaseObject> objectsToRemove = new ArrayList<>();
for (String userId : userIds) {
for (BaseObject object : objects) {
if (object == null) {
continue;
}
String member = object.getStringValue(GROUP_CLASS_MEMBER_FIELD);
if (userId.equals(member)) {
objectsToRemove.add(object);
}
}
}
// Remove them
for (BaseObject object : objectsToRemove) {
groupDoc.removeXObject(object);
}
// Save the document
saveGroupDocument(groupDoc, "Remove some users from the group.");
}
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class DefaultWikiUserManager method addMembers.
@Override
public void addMembers(Collection<String> userIds, String wikiId) throws WikiUserManagerException {
Collection<String> members = getMembers(wikiId);
// Get the group document
XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
// If the group does not contain any user yet, add an empty member (cf: XWIKI-6275).
List<BaseObject> memberObjects = groupDoc.getXObjects(GROUPCLASS_REFERENCE);
if (memberObjects == null || memberObjects.isEmpty()) {
addMemberObject(groupDoc, "");
}
// Add members
for (String userId : userIds) {
if (!members.contains(userId)) {
// Add a member object
addMemberObject(groupDoc, userId);
}
}
// Save the document
saveGroupDocument(groupDoc, "Add members to the group.");
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class DefaultWikiUserManager method getMembers.
@Override
public Collection<String> getMembers(String wikiId) throws WikiUserManagerException {
List<String> members = new ArrayList<>();
try {
// Get the descriptor
WikiDescriptor descriptor = wikiDescriptorManager.getById(wikiId);
// Add the wiki owner
members.add(descriptor.getOwnerId());
} catch (WikiManagerException e) {
throw new WikiUserManagerException(String.format("Failed to get the descriptor for [%s]", wikiId), e);
}
// Get the other members from the wiki AllGroup
XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
List<BaseObject> memberObjects = groupDoc.getXObjects(GROUPCLASS_REFERENCE);
if (memberObjects != null) {
for (BaseObject object : memberObjects) {
if (object == null) {
continue;
}
String member = object.getStringValue(GROUP_CLASS_MEMBER_FIELD);
if (!member.isEmpty() && !members.contains(member)) {
members.add(member);
}
}
}
return members;
}
Aggregations