use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class PratilipiDataUtil method _updatePratilipiPageUrl.
public static Page _updatePratilipiPageUrl(Pratilipi pratilipi) {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Page pratilipiPage = dataAccessor.getPage(PageType.PRATILIPI, pratilipi.getId());
if (pratilipi.getState() == PratilipiState.DELETED) {
if (pratilipiPage != null)
dataAccessor.deletePage(pratilipiPage);
return null;
}
boolean isNew = pratilipiPage == null;
if (isNew) {
pratilipiPage = dataAccessor.newPage();
pratilipiPage.setType(PageType.PRATILIPI);
pratilipiPage.setUri(PageType.PRATILIPI.getUrlPrefix() + pratilipi.getId());
pratilipiPage.setPrimaryContentId(pratilipi.getId());
pratilipiPage.setCreationDate(new Date());
}
String uriPrifix = null;
if (pratilipi.getAuthorId() != null) {
Page authorPage = dataAccessor.getPage(PageType.AUTHOR, pratilipi.getAuthorId());
if (authorPage.getUriAlias() != null)
uriPrifix = authorPage.getUriAlias() + "/";
}
if (uriPrifix == null) {
if (isNew) {
// Do NOT return.
} else if (pratilipiPage.getUriAlias() == null) {
// Do Nothing.
return null;
} else {
logger.log(Level.INFO, "Clearing Pratilipi Page Url: '" + pratilipiPage.getUriAlias() + "' -> 'null'");
pratilipiPage.setUriAlias(null);
}
} else if (pratilipiPage.getUriAlias() == null) {
String uriAlias = UriAliasUtil.generateUriAlias(pratilipiPage.getUriAlias(), uriPrifix, pratilipi.getTitleEn() == null ? pratilipi.getTitle() : pratilipi.getTitleEn());
if (isNew && uriAlias == null) {
// Do NOT return.
// } else if( uriAlias == pratilipiPage.getUriAlias()
// || ( uriAlias != null && uriAlias.equals( pratilipiPage.getUriAlias() ) )
// || ( pratilipiPage.getUriAlias() != null && pratilipiPage.getUriAlias().equals( uriAlias ) ) ) {
// return null; // Do Nothing.
} else {
logger.log(Level.INFO, "Updating Pratilipi Page Url: '" + pratilipiPage.getUriAlias() + "' -> '" + uriAlias + "'");
pratilipiPage.setUriAlias(uriAlias);
}
} else {
// Do Nothing.
return null;
}
return pratilipiPage;
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class PratilipiDataUtil method savePratilipiData.
public static PratilipiData savePratilipiData(PratilipiData pratilipiData) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
_validatePratilipiDataForSave(pratilipiData);
boolean isNew = pratilipiData.getId() == null;
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = isNew ? dataAccessor.newPratilipi() : dataAccessor.getPratilipi(pratilipiData.getId());
if (isNew && !hasAccessToAddPratilipiData(pratilipiData))
throw new InsufficientAccessException();
if (!isNew && !hasAccessToUpdatePratilipiData(pratilipi, pratilipiData))
throw new InsufficientAccessException();
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), isNew ? AccessType.PRATILIPI_ADD : AccessType.PRATILIPI_UPDATE, pratilipi);
if (pratilipiData.hasTitle())
pratilipi.setTitle(pratilipiData.getTitle());
if (pratilipiData.hasTitleEn())
pratilipi.setTitleEn(pratilipiData.getTitleEn());
if (pratilipiData.hasLanguage())
pratilipi.setLanguage(pratilipiData.getLanguage());
// Do NOT update Author for existing content pieces.
if (isNew && pratilipiData.hasAuthorId())
pratilipi.setAuthorId(pratilipiData.getAuthorId());
if (pratilipiData.hasSummary())
pratilipi.setSummary(pratilipiData.getSummary());
if (pratilipiData.hasType())
pratilipi.setType(pratilipiData.getType());
if (pratilipiData.hasContentType())
pratilipi.setContentType(pratilipiData.getContentType());
else if (isNew)
pratilipi.setContentType(PratilipiContentType.PRATILIPI);
if (pratilipiData.hasState())
pratilipi.setState(pratilipiData.getState());
else if (isNew)
pratilipi.setState(PratilipiState.DRAFTED);
if (pratilipiData.hasOldContentFlag())
pratilipi.setOldContent(pratilipiData.isOldContent());
if (isNew)
pratilipi.setListingDate(new Date());
pratilipi.setLastUpdated(new Date());
if (isNew) {
pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
dataAccessor.createOrUpdatePage(_updatePratilipiPageUrl(pratilipi));
} else {
pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, _updatePratilipiPageUrl(pratilipi), auditLog);
}
return createPratilipiData(pratilipi, dataAccessor.getAuthor(pratilipi.getAuthorId()), hasAccessToReadPratilipiMetaData(pratilipi));
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class PratilipiDataUtil method hasAccessToListPratilipiData.
public static boolean hasAccessToListPratilipiData(PratilipiFilter pratilipiFilter) {
if (pratilipiFilter.getState() == PratilipiState.PUBLISHED)
return true;
// Case 2: User with PRATILIPI_LIST access can list Pratilipis in any State.
AccessToken accessToken = AccessTokenFilter.getAccessToken();
if (UserAccessUtil.hasUserAccess(accessToken.getUserId(), pratilipiFilter.getLanguage(), AccessType.PRATILIPI_LIST))
return true;
if (pratilipiFilter.getState() == PratilipiState.DELETED)
return false;
if (pratilipiFilter.getAuthorId() == null)
return false;
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Author author = dataAccessor.getAuthor(pratilipiFilter.getAuthorId());
if (author == null)
return false;
return accessToken.getUserId().equals(author.getUserId());
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class PratilipiDataUtil method savePratilipiCover.
public static String savePratilipiCover(Long pratilipiId, BlobEntry blobEntry) throws InsufficientAccessException, UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
if (!hasAccessToUpdatePratilipiData(pratilipi, null))
throw new InsufficientAccessException();
String coverImageName = new Date().getTime() + "";
BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
blobEntry.setName("pratilipi/" + pratilipiId + "/images/" + coverImageName);
blobAccessor.createOrUpdateBlob(blobEntry);
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
pratilipi.setCoverImage(coverImageName);
pratilipi.setLastUpdated(new Date());
pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
return createPratilipiCoverUrl(pratilipi);
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class CommentDataUtil method saveCommentData.
public static CommentData saveCommentData(CommentData commentData) throws InvalidArgumentException, InsufficientAccessException {
_validateCommentDataForSave(commentData);
boolean isNew = commentData.getId() == null;
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Comment comment = isNew ? dataAccessor.newComment() : dataAccessor.getComment(commentData.getId());
if (isNew && !hasAccessToAddCommentData(commentData))
throw new InsufficientAccessException();
if (!isNew && !hasAccessToUpdateCommentData(comment))
throw new InsufficientAccessException();
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), isNew ? AccessType.COMMENT_ADD : AccessType.COMMENT_UPDATE, comment);
if (isNew) {
comment.setUserId(commentData.getUserId());
comment.setParentType(commentData.getParentType());
comment.setParentId(commentData.getParentId());
if (commentData.getParentType() == CommentParentType.REVIEW) {
UserPratilipi userPratilipi = dataAccessor.getUserPratilipi(commentData.getParentId());
comment.setReferenceType(ReferenceType.PRATILIPI);
comment.setReferenceId(userPratilipi.getPratilipiId());
}
comment.setCreationDate(new Date());
} else {
comment.setLastUpdated(new Date());
}
if (commentData.hasContent())
comment.setContent(commentData.getContent());
if (commentData.hasState())
comment.setState(commentData.getState());
comment = dataAccessor.createOrUpdateComment(comment, auditLog);
User user = dataAccessor.getUser(commentData.getUserId());
UserData userData = UserDataUtil.createUserData(user);
commentData = createCommentData(comment);
commentData.setUser(userData);
return commentData;
}
Aggregations