Search in sources :

Example 1 with UserEntity

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

the class DataAccessorMockImpl method createOrUpdateUser.

@Override
public User createOrUpdateUser(User user) {
    boolean newUser = true;
    for (User aUser : UserMock.USER_TABLE) if (aUser.getId() == user.getId())
        newUser = false;
    if (newUser) {
        Long id = 0L;
        for (User aUser : UserMock.USER_TABLE) if (aUser.getId() >= id)
            id = aUser.getId() + 1;
        ((UserEntity) user).setId(id);
        UserMock.USER_TABLE.add(user);
    } else {
        int index = -1;
        for (int i = 0; i < UserMock.USER_TABLE.size(); i++) if (UserMock.USER_TABLE.get(i).getId() == user.getId())
            index = i;
        UserMock.USER_TABLE.set(index, user);
    }
    return user;
}
Also used : ConversationUser(com.pratilipi.data.type.ConversationUser) User(com.pratilipi.data.type.User) ConversationUserEntity(com.pratilipi.data.type.gae.ConversationUserEntity) UserEntity(com.pratilipi.data.type.gae.UserEntity)

Example 2 with UserEntity

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

the class DataStoreCleanupUtil method delete.

public static void delete(String email, boolean preview) {
    // USER Table
    List<UserEntity> userList = ObjectifyService.ofy().load().type(UserEntity.class).filter("EMAIL", email).list();
    System.out.println("UserEntity # " + userList.size());
    if (userList.size() == 0)
        return;
    for (User user : userList) delete(user, preview);
}
Also used : User(com.pratilipi.data.type.User) UserEntity(com.pratilipi.data.type.gae.UserEntity)

Example 3 with UserEntity

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

the class UserProcessApi method post.

@Post
public GenericResponse post(PostRequest request) throws InvalidArgumentException, UnexpectedServerException {
    if (request.validateData != null && request.validateData) {
        DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
        User user = dataAccessor.getUser(request.userId);
        // DELETED User entity can not have a non-DELETED Author entity linked.
        if (user.getState() == UserState.DELETED) {
            Author author = dataAccessor.getAuthorByUserId(user.getId());
            if (author != null && author.getState() != AuthorState.DELETED)
                throw new InvalidArgumentException("DELETED User entity has a non-DELETED Author entity linked.");
            // TODO: DELETED User entity can not have non-DELETED UserAuthor entities.
            return new GenericResponse();
        }
        // Either of two - email and facebook id - must be present.
        if (user.getEmail() == null && user.getFacebookId() == null)
            throw new InvalidArgumentException("Neither email nor facebook id is set.");
        // Email, if present, must not be empty.
        if (user.getEmail() != null && user.getEmail().trim().isEmpty())
            throw new InvalidArgumentException("Email is empty.");
        // Facebook id, if present, must not be empty.
        if (user.getFacebookId() != null && user.getFacebookId().trim().isEmpty())
            throw new InvalidArgumentException("Facebook Id is empty.");
        // Email, if present, must be trimmed and converted to lower case.
        if (user.getEmail() != null && !user.getEmail().equals(user.getEmail().trim().toLowerCase()))
            throw new InvalidArgumentException("Email is either not trimmed or not converted to lower case.");
        // Only one non-DELETED User entity can exist per email id.
        if (user.getEmail() != null) {
            Query<UserEntity> query = ObjectifyService.ofy().load().type(UserEntity.class).filter("EMAIL", user.getEmail()).filter("STATE !=", UserState.DELETED).order("STATE").order("SIGN_UP_DATE");
            List<UserEntity> list = query.list();
            if (list.size() != 1)
                throw new InvalidArgumentException(list.size() + " non-DELETED User entities found for email " + user.getEmail() + " .");
        }
        // Only one non-DELETED User entity can exist per facebook id.
        if (user.getFacebookId() != null) {
            Query<UserEntity> query = ObjectifyService.ofy().load().type(UserEntity.class).filter("FACEBOOK_ID", user.getFacebookId()).filter("STATE !=", UserState.DELETED).order("STATE").order("SIGN_UP_DATE");
            List<UserEntity> list = query.list();
            if (list.size() != 1)
                throw new InvalidArgumentException(list.size() + " non-DELETED User entities found for Facebook Id " + user.getFacebookId() + " .");
        }
        // Author profile for the user.
        Query<AuthorEntity> query = ObjectifyService.ofy().load().type(AuthorEntity.class).filter("USER_ID", user.getId()).filter("STATE !=", AuthorState.DELETED).order("STATE").order("REGISTRATION_DATE");
        List<AuthorEntity> authorList = query.list();
        if (authorList.size() == 0) {
            if (user.getState() != UserState.REFERRAL || user.getSignUpSource() != UserSignUpSource.PRE_LAUNCH_WEBSITE)
                throw new InvalidArgumentException("Could not find an Author entity linked.");
        } else if (authorList.size() == 1) {
        // Do Nothing.
        } else {
            throw new InvalidArgumentException("User has " + authorList.size() + " non-DELETED Author entities linked.");
        }
    }
    if (request.updateFollowsDoc != null && request.updateFollowsDoc) {
        UserDocUtil.updateUserFollows(request.userId);
    }
    if (request.updateUserAuthorStats != null && request.updateUserAuthorStats) {
        UserDataUtil.updateUserAuthorStats(request.userId);
    }
    return new GenericResponse();
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) Author(com.pratilipi.data.type.Author) UserEntity(com.pratilipi.data.type.gae.UserEntity) AuthorEntity(com.pratilipi.data.type.gae.AuthorEntity) Post(com.pratilipi.api.annotation.Post)

Example 4 with UserEntity

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

the class DataStoreBackupUtil method csvUser.

public static void csvUser() throws UnexpectedServerException {
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessorBackup();
    DateFormat csvDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    csvDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    String CSV_HEADER = "UserId,FacebookId,Email,SignUpDate";
    String CSV_SEPARATOR = ",";
    String LINE_SEPARATOR = "\n";
    StringBuilder csv = new StringBuilder(CSV_HEADER + LINE_SEPARATOR);
    int count = 0;
    QueryResultIterator<UserEntity> itr = ObjectifyService.ofy().cache(false).load().type(UserEntity.class).chunk(1000).iterator();
    while (itr.hasNext()) {
        User user = itr.next();
        csv.append("'" + user.getId().toString()).append(CSV_SEPARATOR).append(user.getFacebookId() == null ? "" : "'" + user.getFacebookId()).append(CSV_SEPARATOR).append(user.getEmail() == null ? "" : user.getEmail()).append(CSV_SEPARATOR).append(csvDateFormat.format(user.getSignUpDate())).append(LINE_SEPARATOR);
        count++;
        if (count % 1000 == 0)
            System.out.println(count + " ...");
    }
    System.out.println(count + " ...");
    BlobEntry userCsvEntry = blobAccessor.newBlob("datastore/user.csv", csv.toString().getBytes(Charset.forName("UTF-8")), "text/csv");
    blobAccessor.createOrUpdateBlob(userCsvEntry);
}
Also used : User(com.pratilipi.data.type.User) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) BlobEntry(com.pratilipi.data.type.BlobEntry) BlobAccessor(com.pratilipi.data.BlobAccessor) SimpleDateFormat(java.text.SimpleDateFormat) UserEntity(com.pratilipi.data.type.gae.UserEntity)

Example 5 with UserEntity

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

the class DataAccessorGaeImpl method getUserList.

@SuppressWarnings("unchecked")
private <T> DataListCursorTuple<T> getUserList(String cursorStr, Integer resultCount, boolean idOnly) {
    Query<UserEntity> query = ObjectifyService.ofy().load().type(UserEntity.class);
    if (cursorStr != null)
        query = query.startAt(Cursor.fromWebSafeString(cursorStr));
    if (resultCount != null && resultCount > 0)
        query = query.limit(resultCount);
    List<T> dataList = resultCount == null ? new ArrayList<T>() : new ArrayList<T>(resultCount);
    Cursor cursor = null;
    if (idOnly) {
        QueryResultIterator<Key<UserEntity>> iterator = query.keys().iterator();
        while (iterator.hasNext()) dataList.add((T) (Long) iterator.next().getId());
        cursor = iterator.getCursor();
    } else {
        QueryResultIterator<UserEntity> iterator = query.iterator();
        while (iterator.hasNext()) dataList.add((T) iterator.next());
        cursor = iterator.getCursor();
    }
    return new DataListCursorTuple<T>(dataList, cursor == null ? null : cursor.toWebSafeString());
}
Also used : Cursor(com.google.appengine.api.datastore.Cursor) ConversationUserEntity(com.pratilipi.data.type.gae.ConversationUserEntity) UserEntity(com.pratilipi.data.type.gae.UserEntity) Key(com.googlecode.objectify.Key)

Aggregations

UserEntity (com.pratilipi.data.type.gae.UserEntity)6 User (com.pratilipi.data.type.User)4 Key (com.googlecode.objectify.Key)2 GenericResponse (com.pratilipi.api.shared.GenericResponse)2 DataAccessor (com.pratilipi.data.DataAccessor)2 ConversationUserEntity (com.pratilipi.data.type.gae.ConversationUserEntity)2 Cursor (com.google.appengine.api.datastore.Cursor)1 Get (com.pratilipi.api.annotation.Get)1 Post (com.pratilipi.api.annotation.Post)1 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)1 BlobAccessor (com.pratilipi.data.BlobAccessor)1 AppProperty (com.pratilipi.data.type.AppProperty)1 Author (com.pratilipi.data.type.Author)1 BlobEntry (com.pratilipi.data.type.BlobEntry)1 ConversationUser (com.pratilipi.data.type.ConversationUser)1 AuthorEntity (com.pratilipi.data.type.gae.AuthorEntity)1 Task (com.pratilipi.taskqueue.Task)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1