Search in sources :

Example 1 with AuthorFilter

use of com.pratilipi.common.util.AuthorFilter in project pratilipi by Pratilipi.

the class AuthorBackupApi method get.

@Get
public GenericResponse get(GetRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessorBackup();
    Date backupDate = new Date();
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    DateFormat csvDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm-z");
    dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    csvDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    dateTimeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    StringBuilder backup = new StringBuilder();
    StringBuilder csv = new StringBuilder(CSV_HEADER + LINE_SEPARATOR);
    int count = 0;
    AuthorFilter authorFilter = new AuthorFilter();
    String cursor = null;
    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new GsonIstDateAdapter()).create();
    while (true) {
        DataListCursorTuple<Author> authorListCursorTupe = dataAccessor.getAuthorList(authorFilter, cursor, 1000);
        List<Author> authorList = authorListCursorTupe.getDataList();
        for (Author author : authorList) {
            backup.append(gson.toJson(author) + LINE_SEPARATOR);
            if (request.generateCsv != null && request.generateCsv)
                csv.append("'" + author.getId()).append(CSV_SEPARATOR).append(author.getUserId() == null ? "" : "'" + author.getUserId()).append(CSV_SEPARATOR).append(author.getFirstName() == null ? "" : author.getFirstName()).append(CSV_SEPARATOR).append(author.getLastName() == null ? "" : author.getLastName()).append(CSV_SEPARATOR).append(author.getPenName() == null ? "" : author.getPenName()).append(CSV_SEPARATOR).append(author.getFirstNameEn() == null ? "" : author.getFirstNameEn()).append(CSV_SEPARATOR).append(author.getLastNameEn() == null ? "" : author.getLastNameEn()).append(CSV_SEPARATOR).append(author.getPenNameEn() == null ? "" : author.getPenNameEn()).append(CSV_SEPARATOR).append(author.getLanguage()).append(CSV_SEPARATOR).append(author.getSummary() != null && author.getSummary().trim().length() != 0).append(CSV_SEPARATOR).append(author.getContentPublished()).append(CSV_SEPARATOR).append(csvDateFormat.format(author.getRegistrationDate())).append(LINE_SEPARATOR);
        }
        count = count + authorList.size();
        if (authorList.size() < 1000)
            break;
        else
            cursor = authorListCursorTupe.getCursor();
    }
    String fileName = "datastore.author/" + dateFormat.format(backupDate) + "/" + "author-" + dateTimeFormat.format(backupDate);
    BlobEntry authorBackupEntry = blobAccessor.newBlob(fileName, backup.toString().getBytes(Charset.forName("UTF-8")), "text/plain");
    blobAccessor.createOrUpdateBlob(authorBackupEntry);
    if (request.generateCsv != null && request.generateCsv) {
        BlobEntry authorCsvEntry = blobAccessor.newBlob("datastore/author.csv", csv.toString().getBytes(Charset.forName("UTF-8")), "text/csv");
        blobAccessor.createOrUpdateBlob(authorCsvEntry);
    }
    logger.log(Level.INFO, "Backed up " + count + " Author Entities.");
    return new GenericResponse();
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) AuthorFilter(com.pratilipi.common.util.AuthorFilter) Gson(com.google.gson.Gson) Date(java.util.Date) GsonIstDateAdapter(com.pratilipi.common.util.GsonIstDateAdapter) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) BlobAccessor(com.pratilipi.data.BlobAccessor) Author(com.pratilipi.data.type.Author) SimpleDateFormat(java.text.SimpleDateFormat) Get(com.pratilipi.api.annotation.Get)

Example 2 with AuthorFilter

use of com.pratilipi.common.util.AuthorFilter in project pratilipi by Pratilipi.

the class AuthorListApi method get.

@Get
public Response get(GetRequest request) throws InsufficientAccessException {
    AuthorFilter authorFilter = new AuthorFilter();
    authorFilter.setLanguage(request.getLanguage());
    authorFilter.setOrderByContentPublished(request.getOrderByContentPublished());
    DataListCursorTuple<AuthorData> authorListCursorTuple = AuthorDataUtil.getAuthorDataList(request.getSearchQuery(), authorFilter, request.getCursor(), request.getResultCount() == null ? 20 : request.getResultCount());
    return new Response(authorListCursorTuple.getDataList(), authorListCursorTuple.getCursor());
}
Also used : GenericResponse(com.pratilipi.api.shared.GenericResponse) AuthorFilter(com.pratilipi.common.util.AuthorFilter) AuthorData(com.pratilipi.data.client.AuthorData) Get(com.pratilipi.api.annotation.Get)

Example 3 with AuthorFilter

use of com.pratilipi.common.util.AuthorFilter in project pratilipi by Pratilipi.

the class BatchProcessApi method post.

@Post
public GenericResponse post(PostRequest request) throws InvalidArgumentException, InsufficientAccessException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    if (request.type == BatchProcessType.NOTIFACTION_BY_AUTHOR_FILTER) {
        Page page = null;
        JsonObject errorMessages = new JsonObject();
        if (request.message == null || request.message.trim().isEmpty())
            errorMessages.addProperty("message", "message is required !");
        if (request.sourceUri == null || request.sourceUri.trim().isEmpty())
            errorMessages.addProperty("sourceUri", "sourceUri is required !");
        else if ((page = dataAccessor.getPage(request.sourceUri)) == null)
            errorMessages.addProperty("sourceUri", "Invalid url !");
        else if (page.getType() != PageType.PRATILIPI)
            errorMessages.addProperty("sourceUri", "Must be a content url !");
        if (errorMessages.size() != 0)
            throw new InvalidArgumentException(errorMessages);
        AuthorFilter authorFilter = new AuthorFilter();
        authorFilter.setLanguage(request.language);
        authorFilter.setState(AuthorState.ACTIVE);
        // Creating Init Doc
        JsonObject initDoc = new JsonObject();
        initDoc.add("authorFilter", new Gson().toJsonTree(authorFilter));
        // Creating Exec Doc
        JsonObject execDoc = new JsonObject();
        execDoc.addProperty("message", request.message);
        execDoc.addProperty("sourceId", page.getPrimaryContentId().toString());
        execDoc.addProperty("type", NotificationType.PRATILIPI.toString());
        BatchProcessDataUtil.createBatchProcess(request.type, initDoc.toString(), execDoc.toString(), request.language);
    }
    return new GenericResponse();
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) AuthorFilter(com.pratilipi.common.util.AuthorFilter) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) Page(com.pratilipi.data.type.Page) Post(com.pratilipi.api.annotation.Post)

Example 4 with AuthorFilter

use of com.pratilipi.common.util.AuthorFilter in project pratilipi by Pratilipi.

the class BatchProcessDataUtil method _execStateGetUserIdsByAuthorFilter.

private static void _execStateGetUserIdsByAuthorFilter(BatchProcess batchProcess) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    BatchProcessDoc processDoc = docAccessor.getBatchProcessDoc(batchProcess.getId());
    AuthorFilter authorFilter = processDoc.getData(BatchProcessState.GET_USER_IDS_BY_AUTHOR_FILTER.getInputName(), AuthorFilter.class);
    Set<Long> userIdSet = new HashSet<>();
    List<Author> tempAuthorList = null;
    String cursor = null;
    int resultCount = 200;
    do {
        DataListCursorTuple<Author> authorListCusorTuple = dataAccessor.getAuthorList(authorFilter, cursor, resultCount);
        tempAuthorList = authorListCusorTuple.getDataList();
        for (Author author : tempAuthorList) if (author.getUserId() != null)
            userIdSet.add(author.getUserId());
        cursor = authorListCusorTuple.getCursor();
    } while (tempAuthorList.size() >= resultCount);
    processDoc.setData(BatchProcessState.GET_USER_IDS_BY_AUTHOR_FILTER.getOutputName(), userIdSet);
    // Saving Doc
    docAccessor.save(batchProcess.getId(), processDoc);
    batchProcess.setStateCompleted(BatchProcessState.GET_USER_IDS_BY_AUTHOR_FILTER);
}
Also used : BatchProcessDoc(com.pratilipi.data.type.BatchProcessDoc) DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) AuthorFilter(com.pratilipi.common.util.AuthorFilter) Author(com.pratilipi.data.type.Author) HashSet(java.util.HashSet)

Example 5 with AuthorFilter

use of com.pratilipi.common.util.AuthorFilter in project pratilipi by Pratilipi.

the class AuthorProcessApi method get.

@Get
public GenericResponse get(GenericRequest request) {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    // Fetching AppProperty
    String appPropertyId = "Api.AuthorProcess.ValidateData";
    AppProperty appProperty = dataAccessor.getAppProperty(appPropertyId);
    if (appProperty == null) {
        appProperty = dataAccessor.newAppProperty(appPropertyId);
        appProperty.setValue(new Date(0));
    }
    // Fetching list of author ids.
    AuthorFilter authorFilter = new AuthorFilter();
    authorFilter.setMinLastUpdate((Date) appProperty.getValue(), false);
    List<Long> authorIdList = dataAccessor.getAuthorIdList(authorFilter, null, 10000).getDataList();
    // Updating AppProperty.
    if (authorIdList.size() > 0) {
        appProperty.setValue(dataAccessor.getAuthor(authorIdList.get(authorIdList.size() - 1)).getLastUpdated());
        appProperty = dataAccessor.createOrUpdateAppProperty(appProperty);
    }
    return new GenericResponse();
}
Also used : GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) AuthorFilter(com.pratilipi.common.util.AuthorFilter) AppProperty(com.pratilipi.data.type.AppProperty) Date(java.util.Date) Get(com.pratilipi.api.annotation.Get)

Aggregations

AuthorFilter (com.pratilipi.common.util.AuthorFilter)5 GenericResponse (com.pratilipi.api.shared.GenericResponse)4 DataAccessor (com.pratilipi.data.DataAccessor)4 Get (com.pratilipi.api.annotation.Get)3 Gson (com.google.gson.Gson)2 Author (com.pratilipi.data.type.Author)2 Date (java.util.Date)2 GsonBuilder (com.google.gson.GsonBuilder)1 JsonObject (com.google.gson.JsonObject)1 Post (com.pratilipi.api.annotation.Post)1 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)1 GsonIstDateAdapter (com.pratilipi.common.util.GsonIstDateAdapter)1 BlobAccessor (com.pratilipi.data.BlobAccessor)1 DocAccessor (com.pratilipi.data.DocAccessor)1 AuthorData (com.pratilipi.data.client.AuthorData)1 AppProperty (com.pratilipi.data.type.AppProperty)1 BatchProcessDoc (com.pratilipi.data.type.BatchProcessDoc)1 BlobEntry (com.pratilipi.data.type.BlobEntry)1 Page (com.pratilipi.data.type.Page)1 DateFormat (java.text.DateFormat)1