Search in sources :

Example 36 with UnexpectedServerException

use of com.pratilipi.common.exception.UnexpectedServerException 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 37 with UnexpectedServerException

use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.

the class ConversationDataUtil method saveMessage.

public static void saveMessage(ContactTeam team, Long userId, String name, String email, String phone, String message, JsonObject data) throws InvalidArgumentException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = dataAccessor.getUser(userId);
    Author author = dataAccessor.getAuthorByUserId(userId);
    Conversation conversation = dataAccessor.getConversation(team, userId);
    if (conversation == null)
        dataAccessor.getConversation(team, email);
    if (conversation != null) {
    // Do Nothing !
    } else if (user != null && (user.getState() == UserState.ACTIVE || user.getState() == UserState.REGISTERED)) {
        // &&
        // conversation
        // ==
        // null
        conversation = dataAccessor.newConversation(team, userId);
        conversation.setCreator(userId);
        conversation.setCreatorName(name);
        conversation.setCreatorEmail(email);
        conversation.setCreatorPhone(phone);
        conversation.setCreationDate(new Date());
        List<ConversationUser> conversationUserList = new ArrayList<>(team.getUserIds().length + 1);
        conversationUserList.add(dataAccessor.newConversationUser(conversation.getId(), userId));
        for (Long recipientUserId : team.getUserIds()) conversationUserList.add(dataAccessor.newConversationUser(conversation.getId(), recipientUserId));
        conversation = dataAccessor.createOrUpdateConversation(conversation, conversationUserList);
    } else if (email != null) {
        // && conversation == null
        conversation = dataAccessor.newConversation(team, email);
        conversation.setCreatorName(name);
        conversation.setCreatorEmail(email);
        conversation.setCreatorPhone(phone);
        conversation.setCreationDate(new Date());
        List<ConversationUser> conversationUserList = new ArrayList<>(team.getUserIds().length + 1);
        for (Long recipientUserId : team.getUserIds()) conversationUserList.add(dataAccessor.newConversationUser(conversation.getId(), recipientUserId));
        conversation = dataAccessor.createOrUpdateConversation(conversation, conversationUserList);
    } else {
        throw new InvalidArgumentException("Valid 'email' is required.");
    }
    conversation.setLastUpdated(new Date());
    ConversationMessage conversationMessage = dataAccessor.newConversationMessage();
    conversationMessage.setConversationId(conversation.getId());
    conversationMessage.setCreatorId(userId);
    conversationMessage.setMessage(message);
    conversationMessage.setData(data);
    conversationMessage.setCreationDate(new Date());
    conversation = dataAccessor.createOrUpdateConversation(conversation, conversationMessage);
    String language = author != null ? author.getLanguage().getNameEn().toLowerCase() : null;
    try {
        ArrayList<String> receiverList = createReceiversId(team.name().toLowerCase(), language);
        createSupportMailTask(receiverList, userId.toString(), name, email, phone, message, data, team.name(), language);
    } catch (UnsupportedEncodingException | UnexpectedServerException e) {
        logger.log(Level.SEVERE, "Exception while creating conversation mail task");
        logger.log(Level.SEVERE, "User ID : " + userId);
        logger.log(Level.SEVERE, "Conversation Id : " + conversation.getId());
        e.printStackTrace();
    }
}
Also used : ConversationUser(com.pratilipi.data.type.ConversationUser) User(com.pratilipi.data.type.User) DataAccessor(com.pratilipi.data.DataAccessor) ConversationMessage(com.pratilipi.data.type.ConversationMessage) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Conversation(com.pratilipi.data.type.Conversation) Date(java.util.Date) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) Author(com.pratilipi.data.type.Author) ArrayList(java.util.ArrayList) List(java.util.List) ConversationUser(com.pratilipi.data.type.ConversationUser)

Example 38 with UnexpectedServerException

use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.

the class UserPratilipiBackfillApi method updateUserPratilipi.

private UserPratilipiData updateUserPratilipi(Long userId, Long pratilipiId, String lastPageOpened, String lastOpenedDate) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    UserPratilipi userPratilipi = dataAccessor.getUserPratilipi(userId, pratilipiId);
    if (userPratilipi == null) {
        // CREATE USER PRATILIPI
        userPratilipi = dataAccessor.newUserPratilipi();
        userPratilipi.setUserId(userId);
        userPratilipi.setPratilipiId(pratilipiId);
    }
    // Creating Auditlog entity
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_PRATILIPI_REVIEW, userPratilipi);
    // update lastPageOpened
    userPratilipi.setLastOpenedPage(lastPageOpened);
    try {
        // update lastPageOpenedDate
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        formatter.setTimeZone(TimeZone.getTimeZone("IST"));
        Date date = (Date) formatter.parse(lastOpenedDate);
        userPratilipi.setLastOpenedDate(date);
        userPratilipi = dataAccessor.createOrUpdateUserPratilipi(userPratilipi, auditLog);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        logger.log(Level.SEVERE, "Event Date : " + lastOpenedDate);
        throw new UnexpectedServerException("Error while parsing date.");
    }
    return UserPratilipiDataUtil.createUserPratilipiData(userPratilipi);
}
Also used : UserPratilipi(com.pratilipi.data.type.UserPratilipi) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) DataAccessor(com.pratilipi.data.DataAccessor) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 39 with UnexpectedServerException

use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.

the class PratilipiTagsUpdateApi method updatePratilipiTags.

@Post
public PostResponse updatePratilipiTags(PostRequest request) throws InsufficientAccessException, UnexpectedServerException {
    List<Long> tagIds = request.getTagIds();
    List<String> suggestedTags = request.getSuggestedTags();
    PratilipiType pratilipiType = request.getPratilipiType();
    Long pratilipiId = request.getPratilipiId();
    Logger.getLogger(PratilipiTagsUpdateApi.class.getSimpleName()).log(Level.INFO, "TagIds : " + tagIds);
    Logger.getLogger(PratilipiTagsUpdateApi.class.getSimpleName()).log(Level.INFO, "SuggestedTags : " + suggestedTags);
    if (tagIds == null && suggestedTags == null)
        throw new UnexpectedServerException("Both tagIds and suggestedTags cannot be null");
    boolean isUpdated = PratilipiDataUtil.updatePratilipiTags(pratilipiId, pratilipiType, tagIds, suggestedTags);
    return new PostResponse(isUpdated);
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) PratilipiType(com.pratilipi.common.type.PratilipiType) Post(com.pratilipi.api.annotation.Post)

Example 40 with UnexpectedServerException

use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.

the class RtdbAccessorFirebaseImpl method _getUserPreferences.

private Map<Long, UserPreferenceRtdb> _getUserPreferences(Map<String, String> paramsMap) throws UnexpectedServerException {
    try {
        BlobEntry blobEntry = HttpUtil.doGet(_getUserPreferenceDbUrl(), headersMap, paramsMap);
        String jsonStr = new String(blobEntry.getData(), "UTF-8");
        JsonObject json = new Gson().fromJson(jsonStr, JsonElement.class).getAsJsonObject();
        Map<Long, UserPreferenceRtdb> userPreferenceMap = new HashMap<>();
        for (Entry<String, JsonElement> entry : json.entrySet()) userPreferenceMap.put(Long.parseLong(entry.getKey()), _getUserPreferenceRtdb(entry.getValue().getAsJsonObject()));
        return userPreferenceMap;
    } catch (UnsupportedEncodingException e) {
        logger.log(Level.SEVERE, e.getMessage());
        throw new UnexpectedServerException();
    }
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) BlobEntry(com.pratilipi.data.type.BlobEntry) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UserPreferenceRtdb(com.pratilipi.data.type.UserPreferenceRtdb)

Aggregations

UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)46 IOException (java.io.IOException)19 JsonObject (com.google.gson.JsonObject)12 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)12 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 HashMap (java.util.HashMap)12 Gson (com.google.gson.Gson)10 DataAccessor (com.pratilipi.data.DataAccessor)10 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)6 BlobEntry (com.pratilipi.data.type.BlobEntry)6 Date (java.util.Date)6 File (java.io.File)5 JsonElement (com.google.gson.JsonElement)4 Get (com.pratilipi.api.annotation.Get)4 Post (com.pratilipi.api.annotation.Post)4 GenericResponse (com.pratilipi.api.shared.GenericResponse)4 OutputStream (java.io.OutputStream)4 URL (java.net.URL)4 GcsFilename (com.google.appengine.tools.cloudstorage.GcsFilename)3 Page (com.pratilipi.data.type.Page)3