Search in sources :

Example 1 with Get

use of com.pratilipi.api.annotation.Get 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 Get

use of com.pratilipi.api.annotation.Get in project pratilipi by Pratilipi.

the class AuthorApi method get.

@Get
public Response get(GetRequest request) {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Author author = dataAccessor.getAuthor(request.authorId);
    UserAuthor userAuthor = dataAccessor.getUserAuthor(AccessTokenFilter.getAccessToken().getUserId(), request.authorId);
    AuthorData authorData = AuthorDataUtil.createAuthorData(author, null, null);
    Response response = new Response(authorData);
    response.setFollowing(userAuthor != null && userAuthor.getFollowState() == UserFollowState.FOLLOWING);
    return response;
}
Also used : GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) AuthorData(com.pratilipi.data.client.AuthorData) UserAuthor(com.pratilipi.data.type.UserAuthor) Author(com.pratilipi.data.type.Author) UserAuthor(com.pratilipi.data.type.UserAuthor) Get(com.pratilipi.api.annotation.Get)

Example 3 with Get

use of com.pratilipi.api.annotation.Get in project pratilipi by Pratilipi.

the class EventApi method get.

@Get
public Response get(GetRequest request) {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Event event = dataAccessor.getEvent(request.eventId);
    EventData eventData = EventDataUtil.createEventData(event, true);
    return new Response(eventData);
}
Also used : GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) Event(com.pratilipi.data.type.Event) EventData(com.pratilipi.data.client.EventData) Get(com.pratilipi.api.annotation.Get)

Example 4 with Get

use of com.pratilipi.api.annotation.Get in project pratilipi by Pratilipi.

the class PratilipiV1Api method get.

@Get
public Response get(GetRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(request.pratilipiId);
    Author author = pratilipi.getAuthorId() == null ? null : dataAccessor.getAuthor(pratilipi.getAuthorId());
    PratilipiData pratilipiData = PratilipiDataUtil.createPratilipiData(pratilipi, author);
    if (UxModeFilter.isAndroidApp()) {
        DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
        PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(request.pratilipiId);
        pratilipiData.setIndex(pcDoc == null ? null : pcDoc.getIndex());
    }
    return new Response(pratilipiData);
}
Also used : GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) PratilipiData(com.pratilipi.data.client.PratilipiData) DocAccessor(com.pratilipi.data.DocAccessor) Author(com.pratilipi.data.type.Author) Pratilipi(com.pratilipi.data.type.Pratilipi) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc) Get(com.pratilipi.api.annotation.Get)

Example 5 with Get

use of com.pratilipi.api.annotation.Get in project pratilipi by Pratilipi.

the class PratilipiV2Api method get.

@Get
public Response get(GetRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(request.pratilipiId);
    Author author = pratilipi.getAuthorId() == null ? null : dataAccessor.getAuthor(pratilipi.getAuthorId());
    return new Response(PratilipiDataUtil.createPratilipiData(pratilipi, author));
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) Author(com.pratilipi.data.type.Author) Pratilipi(com.pratilipi.data.type.Pratilipi) Get(com.pratilipi.api.annotation.Get)

Aggregations

Get (com.pratilipi.api.annotation.Get)34 GenericResponse (com.pratilipi.api.shared.GenericResponse)27 DataAccessor (com.pratilipi.data.DataAccessor)22 Date (java.util.Date)11 PratilipiFilter (com.pratilipi.common.util.PratilipiFilter)7 ArrayList (java.util.ArrayList)7 PratilipiData (com.pratilipi.data.client.PratilipiData)6 AppProperty (com.pratilipi.data.type.AppProperty)5 Author (com.pratilipi.data.type.Author)5 Task (com.pratilipi.taskqueue.Task)5 SimpleDateFormat (java.text.SimpleDateFormat)5 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)4 Event (com.pratilipi.data.type.Event)4 Pratilipi (com.pratilipi.data.type.Pratilipi)4 Gson (com.google.gson.Gson)3 JsonObject (com.google.gson.JsonObject)3 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)3 AuthorFilter (com.pratilipi.common.util.AuthorFilter)3 BlobAccessor (com.pratilipi.data.BlobAccessor)3 DocAccessor (com.pratilipi.data.DocAccessor)3