Search in sources :

Example 1 with PratilipiEntity

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

the class PratilipiBackupApi method get.

@Get
public GenericResponse get(GetRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    // Fetching Cursor from AppProperty
    AppProperty appProperty = dataAccessor.getAppProperty(AppProperty.API_PRATILIPI_BACKUP);
    if (appProperty == null)
        appProperty = dataAccessor.newAppProperty(AppProperty.API_PRATILIPI_BACKUP);
    Cursor cursor = appProperty.getValue() == null ? null : Cursor.fromWebSafeString((String) appProperty.getValue());
    QueryResultIterator<Key<PratilipiEntity>> itr = ObjectifyService.ofy().load().type(PratilipiEntity.class).filter("LAST_UPDATED >=", // Thu Dec 01 00:00:00 IST 2016
    new Date(1480530600000L)).order("LAST_UPDATED").startAt(cursor).keys().iterator();
    int batchSize = 1000;
    List<Task> taskList = new ArrayList<>(batchSize);
    while (itr.hasNext()) {
        TaskQueueFactory.getPratilipiTaskQueue().add(TaskQueueFactory.newTask().setUrl("/pratilipi/backup").addParam("pratilipiId", itr.next().getId() + ""));
        if (taskList.size() == batchSize || !itr.hasNext()) {
            TaskQueueFactory.getPratilipiTaskQueue().addAll(taskList);
            taskList.clear();
        }
    }
    // Updating Cursor to AppProperty
    appProperty.setValue(itr.getCursor().toWebSafeString());
    dataAccessor.createOrUpdateAppProperty(appProperty);
    /*		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;
		String cursor = null;
		PratilipiFilter pratilipiFilter = new PratilipiFilter();
		Gson gson = new GsonBuilder().registerTypeAdapter( Date.class, new GsonIstDateAdapter() ).create();
		
		while( true ) {
			DataListCursorTuple<Pratilipi> pratilipiListCursorTuple = dataAccessor.getPratilipiList( pratilipiFilter, cursor, 1000 );
			List<Pratilipi> pratilipiList = pratilipiListCursorTuple.getDataList();

			for( Pratilipi pratilipi : pratilipiList ) {
                backup.append( gson.toJson( pratilipi ) + LINE_SEPARATOR );

				if( request.generateCsv != null && request.generateCsv )
					csv.append( "'" + pratilipi.getId() )
							.append( CSV_SEPARATOR ).append( pratilipi.getAuthorId() == null ? "" : "'" + pratilipi.getAuthorId() )
							.append( CSV_SEPARATOR ).append( pratilipi.getTitle()	 == null ? "" : "\"" + pratilipi.getTitle().replace( "\"", "\"\"" ) + "\"" )
							.append( CSV_SEPARATOR ).append( pratilipi.getTitleEn()	 == null ? "" : "\"" + pratilipi.getTitleEn().replace( "\"", "\"\"" ) + "\"" )
							.append( CSV_SEPARATOR ).append( pratilipi.getLanguage() )
							.append( CSV_SEPARATOR ).append( pratilipi.getSummary() != null && pratilipi.getSummary().trim().length() != 0 )
							.append( CSV_SEPARATOR ).append( pratilipi.getType() )
							.append( CSV_SEPARATOR ).append( pratilipi.getContentType() )
							.append( CSV_SEPARATOR ).append( pratilipi.getState() )
							.append( CSV_SEPARATOR ).append( pratilipi.getCoverImage() != null )
							.append( CSV_SEPARATOR ).append( csvDateFormat.format( pratilipi.getListingDate() ) )
							.append( CSV_SEPARATOR ).append( pratilipi.getReviewCount() )
							.append( CSV_SEPARATOR ).append( pratilipi.getRatingCount() )
							.append( CSV_SEPARATOR ).append( pratilipi.getTotalRating() )
							.append( CSV_SEPARATOR ).append( pratilipi.getReadCount() )
							.append( LINE_SEPARATOR );
				
			}
			
			count = count + pratilipiList.size();

			if( pratilipiList.size() < 1000 )
				break;
			else
				cursor = pratilipiListCursorTuple.getCursor();
		}
		

		String fileName = "datastore.pratilipi/"
				+ dateFormat.format( backupDate ) + "/"
				+ "pratilipi-" + dateTimeFormat.format( backupDate );

		BlobEntry pratilipiBackupEntry = blobAccessor.newBlob(
				fileName,
				backup.toString().getBytes( Charset.forName( "UTF-8" ) ),
				"text/plain" );
		
		blobAccessor.createOrUpdateBlob( pratilipiBackupEntry );
		
		
		if( request.generateCsv != null && request.generateCsv ) {
			BlobEntry authorCsvEntry = blobAccessor.newBlob(
					"datastore/pratilipi.csv",
					csv.toString().getBytes( Charset.forName( "UTF-8" ) ),
					"text/csv" );
			blobAccessor.createOrUpdateBlob( authorCsvEntry );
		}
		

		logger.log( Level.INFO, "Backed up " + count + " Pratilipi Entities." );
*/
    return new GenericResponse();
}
Also used : Task(com.pratilipi.taskqueue.Task) PratilipiEntity(com.pratilipi.data.type.gae.PratilipiEntity) GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) Cursor(com.google.appengine.api.datastore.Cursor) Date(java.util.Date) AppProperty(com.pratilipi.data.type.AppProperty) Key(com.googlecode.objectify.Key) Get(com.pratilipi.api.annotation.Get)

Example 2 with PratilipiEntity

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

the class DataStoreBackupUtil method csvPratilipi.

public static void csvPratilipi() throws UnexpectedServerException {
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessorBackup();
    DateFormat csvDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    csvDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    String CSV_HEADER = "PratilipiId,AuthorId," + "Title,TitleEN,Language,HasSummary," + "Type,ContentType,State,HasCover,ListingDate," + "ReviewCount, RatingCount, TotalRating, ReadCount";
    String CSV_SEPARATOR = ",";
    String LINE_SEPARATOR = "\n";
    StringBuilder csv = new StringBuilder(CSV_HEADER + LINE_SEPARATOR);
    int count = 0;
    QueryResultIterator<PratilipiEntity> itr = ObjectifyService.ofy().cache(false).load().type(PratilipiEntity.class).chunk(1000).iterator();
    while (itr.hasNext()) {
        Pratilipi pratilipi = itr.next();
        csv.append("'" + pratilipi.getId()).append(CSV_SEPARATOR).append(pratilipi.getAuthorId() == null ? "" : "'" + pratilipi.getAuthorId()).append(CSV_SEPARATOR).append(pratilipi.getTitle() == null ? "" : "\"" + pratilipi.getTitle().replace("\"", "\"\"") + "\"").append(CSV_SEPARATOR).append(pratilipi.getTitleEn() == null ? "" : "\"" + pratilipi.getTitleEn().replace("\"", "\"\"") + "\"").append(CSV_SEPARATOR).append(pratilipi.getLanguage()).append(CSV_SEPARATOR).append(pratilipi.getSummary() != null && pratilipi.getSummary().trim().length() != 0).append(CSV_SEPARATOR).append(pratilipi.getType()).append(CSV_SEPARATOR).append(pratilipi.getContentType()).append(CSV_SEPARATOR).append(pratilipi.getState()).append(CSV_SEPARATOR).append(pratilipi.getCoverImage() != null).append(CSV_SEPARATOR).append(csvDateFormat.format(pratilipi.getListingDate())).append(CSV_SEPARATOR).append(pratilipi.getReviewCount()).append(CSV_SEPARATOR).append(pratilipi.getRatingCount()).append(CSV_SEPARATOR).append(pratilipi.getTotalRating()).append(CSV_SEPARATOR).append(pratilipi.getReadCount()).append(LINE_SEPARATOR);
        count++;
        if (count % 1000 == 0)
            System.out.println(count + " ...");
    }
    System.out.println(count + " ...");
    BlobEntry authorCsvEntry = blobAccessor.newBlob("datastore/pratilipi.csv", csv.toString().getBytes(Charset.forName("UTF-8")), "text/csv");
    blobAccessor.createOrUpdateBlob(authorCsvEntry);
}
Also used : PratilipiEntity(com.pratilipi.data.type.gae.PratilipiEntity) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) BlobEntry(com.pratilipi.data.type.BlobEntry) BlobAccessor(com.pratilipi.data.BlobAccessor) Pratilipi(com.pratilipi.data.type.Pratilipi) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with PratilipiEntity

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

the class DataAccessorMockImpl method createOrUpdatePratilipi.

@Override
public Pratilipi createOrUpdatePratilipi(Pratilipi pratilipi, AuditLog auditLog) {
    if (pratilipi.getId() != null)
        return pratilipi;
    long pratilipiId = 0L;
    for (Pratilipi aPratilipi : PRATILIPI_TABLE) if (pratilipiId <= aPratilipi.getId())
        pratilipiId = aPratilipi.getId() + 1;
    ((PratilipiEntity) pratilipi).setId(pratilipiId);
    PRATILIPI_TABLE.add(pratilipi);
    return pratilipi;
}
Also used : PratilipiEntity(com.pratilipi.data.type.gae.PratilipiEntity) UserPratilipiEntity(com.pratilipi.data.type.gae.UserPratilipiEntity) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi)

Example 4 with PratilipiEntity

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

the class DataStoreCleanupUtil method migratePratilipi.

public static void migratePratilipi(Long fromAuthorId, Long toAuthorId) {
    QueryResultIterator<PratilipiEntity> iterator = ObjectifyService.ofy().load().type(PratilipiEntity.class).filter("AUTHOR_ID", fromAuthorId).chunk(100).iterator();
    while (iterator.hasNext()) {
        Pratilipi pratilipi = iterator.next();
        pratilipi.setAuthorId(toAuthorId);
        ObjectifyService.ofy().save().entity(pratilipi).now();
        Task task1 = TaskQueueFactory.newTask().setUrl("/author/process").addParam("authorId", fromAuthorId.toString()).addParam("processData", "true").addParam("updateStats", "true");
        TaskQueueFactory.getAuthorTaskQueue().add(task1);
        Task task2 = TaskQueueFactory.newTask().setUrl("/pratilipi/process").addParam("pratilipiId", pratilipi.getId().toString()).addParam("processData", "true").addParam("updateStats", "true");
        TaskQueueFactory.getPratilipiTaskQueue().add(task2);
        System.out.println("Migrating Pratilipi: " + pratilipi.getId());
    }
}
Also used : Task(com.pratilipi.taskqueue.Task) UserPratilipiEntity(com.pratilipi.data.type.gae.UserPratilipiEntity) PratilipiEntity(com.pratilipi.data.type.gae.PratilipiEntity) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi)

Example 5 with PratilipiEntity

use of com.pratilipi.data.type.gae.PratilipiEntity 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)

Aggregations

PratilipiEntity (com.pratilipi.data.type.gae.PratilipiEntity)5 Pratilipi (com.pratilipi.data.type.Pratilipi)4 UserPratilipi (com.pratilipi.data.type.UserPratilipi)3 UserPratilipiEntity (com.pratilipi.data.type.gae.UserPratilipiEntity)3 Task (com.pratilipi.taskqueue.Task)2 Cursor (com.google.appengine.api.datastore.Cursor)1 Key (com.googlecode.objectify.Key)1 Get (com.pratilipi.api.annotation.Get)1 GenericResponse (com.pratilipi.api.shared.GenericResponse)1 BlobAccessor (com.pratilipi.data.BlobAccessor)1 DataAccessor (com.pratilipi.data.DataAccessor)1 AppProperty (com.pratilipi.data.type.AppProperty)1 BlobEntry (com.pratilipi.data.type.BlobEntry)1 Page (com.pratilipi.data.type.Page)1 UserAuthor (com.pratilipi.data.type.UserAuthor)1 PageEntity (com.pratilipi.data.type.gae.PageEntity)1 UserAuthorEntity (com.pratilipi.data.type.gae.UserAuthorEntity)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1