Search in sources :

Example 31 with Pratilipi

use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.

the class AuthorProcessApi method _validateAuthorData.

private void _validateAuthorData(Long authorId) throws InvalidArgumentException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Author author = dataAccessor.getAuthor(authorId);
    Page page = dataAccessor.getPage(PageType.AUTHOR, authorId);
    // DELETED Author can not have a Page entity linked.
    if (author.getState() == AuthorState.DELETED && page != null)
        throw new InvalidArgumentException("DELETED Author has a non-deleted Page entity.");
    // Non-DELETED Author must have a page entity linked.
    if (author.getState() != AuthorState.DELETED && page == null)
        throw new InvalidArgumentException("Page entity is missing for the Author.");
    // Pratilipi Entities linked with the Author.
    PratilipiFilter pratilipiFilter = new PratilipiFilter();
    pratilipiFilter.setAuthorId(authorId);
    pratilipiFilter.setState(PratilipiState.PUBLISHED);
    List<Pratilipi> pratilipiList = dataAccessor.getPratilipiList(pratilipiFilter, null, null).getDataList();
    // DELETED Author cannot have non-DELETED Pratilipi entities linked.
    if (author.getState() == AuthorState.DELETED) {
        if (pratilipiList.size() != 0)
            throw new InvalidArgumentException("DELETED Author has " + pratilipiList.size() + " non-deleted Pratilipi Entities linked.");
        return;
    }
    // Author having Pratilipi entites linked, can not have his/her language set to null.
    if (author.getLanguage() == null && pratilipiList.size() != 0)
        throw new InvalidArgumentException("Author has " + pratilipiList.size() + " non-deleted Pratilipi Entities linked but his/her language is not set.");
    // Count of Pratilipi Entities in each language.
    Map<Language, Integer> langCount = new HashMap<>();
    for (Pratilipi pratilipi : pratilipiList) {
        if (pratilipi.getState() != PratilipiState.PUBLISHED)
            continue;
        Integer count = langCount.get(pratilipi.getLanguage());
        count = count == null ? 1 : count++;
        langCount.put(pratilipi.getLanguage(), count);
    }
    // Author, having Pratilipi entities in just one language, must have the same set as his/her profile language.
    if (langCount.keySet().size() == 1) {
        Language language = langCount.keySet().iterator().next();
        if (langCount.get(language) > 1 && author.getLanguage() != language)
            throw new InvalidArgumentException("Author has " + author.getLanguage() + " as his/her profile language but all his/her content pieces are in " + language + ".");
    }
    // At least one of four name fields must be set.
    if (author.getFirstName() == null && author.getLastName() == null && author.getFirstNameEn() == null && author.getLastNameEn() == null)
        throw new InvalidArgumentException("Author name is missing.");
    // Name fields must not have empty string.
    if ((author.getFirstName() != null && author.getFirstName().trim().isEmpty()) || (author.getLastName() != null && author.getLastName().trim().isEmpty()) || (author.getFirstNameEn() != null && author.getFirstNameEn().trim().isEmpty()) || (author.getLastNameEn() != null && author.getLastNameEn().trim().isEmpty()))
        throw new InvalidArgumentException("Author name has empty string.");
}
Also used : PratilipiFilter(com.pratilipi.common.util.PratilipiFilter) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) Language(com.pratilipi.common.type.Language) HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor) Author(com.pratilipi.data.type.Author) Page(com.pratilipi.data.type.Page) Pratilipi(com.pratilipi.data.type.Pratilipi)

Example 32 with Pratilipi

use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.

the class PratilipiBackupApi method post.

@Post
public GenericResponse post(PostRequest request) throws UnexpectedServerException {
    Pratilipi pratilipi = DataAccessorFactory.getDataAccessor().getPratilipi(request.pratilipiId);
    Date dateTime = new Date(pratilipi.getLastUpdated().getTime() + TimeUnit.HOURS.toMillis(1L) - 1);
    DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd-HH'.00'-z");
    dateTimeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
    String srcBucket = "static.pratilipi.com";
    String dstBucket = "backup.pratilipi.com";
    String srcPrefix = "pratilipi/" + request.pratilipiId + "/";
    String dstPrefix = srcBucket + "/pratilipi-" + dateTimeFormat.format(dateTime) + "/" + request.pratilipiId + "/";
    try {
        ListResult result = gcsService.list(srcBucket, new ListOptions.Builder().setPrefix(srcPrefix).build());
        while (result.hasNext()) {
            String srcName = result.next().getName();
            String dstName = dstPrefix + srcName.substring(srcPrefix.length());
            gcsService.copy(new GcsFilename(srcBucket, srcName), new GcsFilename(dstBucket, dstName));
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE, "", e);
        throw new UnexpectedServerException();
    }
    return new GenericResponse();
}
Also used : GenericResponse(com.pratilipi.api.shared.GenericResponse) GcsService(com.google.appengine.tools.cloudstorage.GcsService) ListOptions(com.google.appengine.tools.cloudstorage.ListOptions) IOException(java.io.IOException) Date(java.util.Date) ListResult(com.google.appengine.tools.cloudstorage.ListResult) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Pratilipi(com.pratilipi.data.type.Pratilipi) SimpleDateFormat(java.text.SimpleDateFormat) GcsFilename(com.google.appengine.tools.cloudstorage.GcsFilename) Post(com.pratilipi.api.annotation.Post)

Example 33 with Pratilipi

use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.

the class DataStoreCleanupUtil method delete.

public static void delete(Author author, boolean preview) {
    System.out.println();
    System.out.println("Author id: " + author.getId() + ", state: " + author.getState());
    if (!preview && author.getState() != AuthorState.DELETED && author.getState() != AuthorState.BLOCKED) {
        author.setState(AuthorState.DELETED);
        // Save
        ObjectifyService.ofy().save().entity(author).now();
        // Delete search index
        DataAccessorFactory.getSearchAccessor().deleteAuthorDataIndex(author.getId());
    }
    // USER_AUTHOR Table
    List<UserAuthorEntity> userAuthorList = ObjectifyService.ofy().load().type(UserAuthorEntity.class).filter("AUTHOR_ID", author.getId()).list();
    System.out.println("UserAuthorEntity # " + userAuthorList.size());
    int followerCount = 0;
    for (UserAuthor userAuthor : userAuthorList) if (userAuthor.getFollowState() == UserFollowState.FOLLOWING)
        followerCount++;
    System.out.println("Follower ## " + followerCount);
    if (!preview) {
        for (UserAuthor userAuthor : userAuthorList) {
            if (userAuthor.getFollowState() != null) {
                userAuthor.setFollowState(null);
                // Save
                ObjectifyService.ofy().save().entity(userAuthor).now();
            }
        }
    }
    // PAGE Table
    List<PageEntity> pageList = ObjectifyService.ofy().load().type(PageEntity.class).filter("PAGE_TYPE", "AUTHOR").filter("PRIMARY_CONTENT_ID", author.getId()).list();
    System.out.println("PageEntity # " + pageList.size());
    if (!preview)
        for (Page page : pageList) // Delete
        ObjectifyService.ofy().delete().entity(page).now();
    // PRATILIPI Table
    List<PratilipiEntity> pratilipiList = ObjectifyService.ofy().load().type(PratilipiEntity.class).filter("AUTHOR_ID", author.getId()).list();
    System.out.println();
    System.out.println("PratilipiEntity # " + pratilipiList.size());
    if (pratilipiList.size() == 0)
        return;
    for (Pratilipi pratilipi : pratilipiList) delete(pratilipi, preview);
}
Also used : UserAuthorEntity(com.pratilipi.data.type.gae.UserAuthorEntity) PageEntity(com.pratilipi.data.type.gae.PageEntity) UserPratilipiEntity(com.pratilipi.data.type.gae.UserPratilipiEntity) PratilipiEntity(com.pratilipi.data.type.gae.PratilipiEntity) UserAuthor(com.pratilipi.data.type.UserAuthor) Page(com.pratilipi.data.type.Page) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi)

Example 34 with Pratilipi

use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.

the class DataAccessorMockImpl method getPratilipiIdList.

@Override
public DataListCursorTuple<Long> getPratilipiIdList(PratilipiFilter pratilipiFilter, String cursorStr, Integer offset, Integer resultCount) {
    List<Long> pratilipiIdList = new ArrayList<Long>();
    for (Pratilipi pratilipi : PratilipiMock.PRATILIPI_TABLE) {
        if (pratilipi.getLanguage() == pratilipiFilter.getLanguage()) {
            pratilipiIdList.add(pratilipi.getId());
            pratilipiIdList.add(pratilipi.getId());
        }
    }
    return new DataListCursorTuple<>(pratilipiIdList, null);
}
Also used : ArrayList(java.util.ArrayList) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi)

Example 35 with Pratilipi

use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.

the class PratilipiDataUtil method getPratilipiCover.

public static BlobEntry getPratilipiCover(Long pratilipiId, Integer width) throws UnexpectedServerException {
    Pratilipi pratilipi = pratilipiId == null ? null : DataAccessorFactory.getDataAccessor().getPratilipi(pratilipiId);
    BlobEntry blobEntry = null;
    if (pratilipi == null || pratilipi.getCoverImage() == null) {
        blobEntry = DataAccessorFactory.getBlobAccessor().getBlob("pratilipi/default/images/cover");
    } else {
        blobEntry = DataAccessorFactory.getBlobAccessor().getBlob("pratilipi/" + pratilipiId + "/images/" + pratilipi.getCoverImage());
        if (blobEntry == null)
            blobEntry = DataAccessorFactory.getBlobAccessor().getBlob("pratilipi-cover/original/" + pratilipiId);
    }
    if (width != null)
        blobEntry = ImageUtil.resize(blobEntry, width, (int) (1.5 * width));
    return blobEntry;
}
Also used : BlobEntry(com.pratilipi.data.type.BlobEntry) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi)

Aggregations

Pratilipi (com.pratilipi.data.type.Pratilipi)45 DataAccessor (com.pratilipi.data.DataAccessor)36 UserPratilipi (com.pratilipi.data.type.UserPratilipi)33 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)17 PratilipiContentDoc (com.pratilipi.data.type.PratilipiContentDoc)14 DocAccessor (com.pratilipi.data.DocAccessor)13 AuditLog (com.pratilipi.data.type.AuditLog)10 Author (com.pratilipi.data.type.Author)9 Page (com.pratilipi.data.type.Page)9 Chapter (com.pratilipi.data.type.PratilipiContentDoc.Chapter)9 Date (java.util.Date)9 BlobEntry (com.pratilipi.data.type.BlobEntry)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)6 BlobAccessor (com.pratilipi.data.BlobAccessor)6 Get (com.pratilipi.api.annotation.Get)5 GenericResponse (com.pratilipi.api.shared.GenericResponse)5 PratilipiData (com.pratilipi.data.client.PratilipiData)5 PratilipiEntity (com.pratilipi.data.type.gae.PratilipiEntity)4