Last active
August 29, 2015 14:24
-
-
Save jittagornp/527b7a5761393a85341a to your computer and use it in GitHub Desktop.
code flow of pamarin api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pamarin.api.model.page; | |
import com.pamarin.api.model.MetaProperty; | |
import com.pamarin.api.model.page.impl.PageContent; | |
import java.util.List; | |
import org.codehaus.jackson.map.annotate.JsonSerialize; | |
/** | |
* @author jittagornp | |
*/ | |
public class PageData<T extends PageContent> { | |
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) | |
private String title; | |
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) | |
private List<MetaProperty> metadata; | |
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) | |
private T content; | |
public static final PageData EMPTY = new PageData() { | |
@Override | |
public PageData setTitle(String title) { | |
throw new UnsupportedOperationException("Unsupported setTitle method."); | |
} | |
@Override | |
public PageData setMetadata(List metadata) { | |
throw new UnsupportedOperationException("Unsupported setMetadata method."); | |
} | |
@Override | |
public PageData setContent(PageContent content) { | |
throw new UnsupportedOperationException("Unsupported setContent method."); | |
} | |
}; | |
public String getTitle() { | |
return title; | |
} | |
public PageData<T> setTitle(String title) { | |
this.title = title; | |
return this; | |
} | |
public List<MetaProperty> getMetadata() { | |
return metadata; | |
} | |
public PageData<T> setMetadata(List<MetaProperty> metadata) { | |
this.metadata = metadata; | |
return this; | |
} | |
public PageData<T> requireMedata(boolean requireMedata) { | |
if (content == null) { | |
throw new IllegalStateException("require page data content."); | |
} | |
title = content.getTitle(); | |
if (requireMedata) { | |
content.buildPageMetadata(); | |
metadata = content.getMetadata(); | |
} | |
return this; | |
} | |
public PageData<T> modify(){ | |
content.modify(); | |
return this; | |
} | |
public T getContent() { | |
return content; | |
} | |
public PageData<T> setContent(T content) { | |
this.content = content; | |
return this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pamarin.rest.controller; | |
import com.pamarin.api.model.page.PageData; | |
import com.pamarin.rest.service.TripService; | |
import com.pamarin.rest.service.page.TripPageService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.RequestParam; | |
import org.springframework.web.bind.annotation.RestController; | |
/** | |
* @author jittagornp | |
*/ | |
@RestController | |
public class TripCtrl { | |
@Autowired | |
private TripPageService pageService; | |
@RequestMapping( | |
value = "/users/{username}/trips/{tripname}/pagedata", | |
method = RequestMethod.GET | |
) | |
public PageData findPageDataByUsernameAndTripname( | |
@PathVariable("username") String username, | |
@PathVariable("tripname") String tripname, | |
@RequestParam( | |
value = "metadata", | |
required = false, | |
defaultValue = "false" | |
) boolean requireMetadata | |
) { | |
return pageService.findByUsernameAndTripname( | |
username, | |
tripname, | |
requireMetadata | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pamarin.api.model.page.impl; | |
import com.pamarin.api.domain.impl.TripPostPageResource; | |
import com.pamarin.api.model.entity.Trip; | |
import com.pamarin.api.page.metadata.MetaTag; | |
import com.pamarin.api.page.metadata.OpengraphProtocol; | |
import com.pamarin.api.page.PageTitle; | |
import com.pamarin.api.page.metadata.GooglePlusSnippet; | |
import com.pamarin.api.page.metadata.TwitterCardsMarkup; | |
import org.codehaus.jackson.annotate.JsonProperty; | |
import org.codehaus.jackson.map.annotate.JsonSerialize; | |
import static org.springframework.util.StringUtils.hasText; | |
/** | |
* @author jittagornp | |
*/ | |
public class TripPageContent extends PageContent { | |
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) | |
private Trip trip; | |
@JsonProperty("post_page") | |
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) | |
private TripPostPageResource postPage; | |
public Trip getTrip() { | |
return trip; | |
} | |
public TripPageContent setTrip(Trip trip) { | |
this.trip = trip; | |
return this; | |
} | |
public TripPostPageResource getPostPage() { | |
return postPage; | |
} | |
public TripPageContent setPostPage(TripPostPageResource postPage) { | |
this.postPage = postPage; | |
return this; | |
} | |
private String getDescription() { | |
if (hasText(trip.getDescription())) { | |
return trip.getDescription(); | |
} | |
return trip.getName(); | |
} | |
private void htmlMetadata() { | |
addPageMetadata(MetaTag.require( | |
"key", | |
getDescription() | |
)); | |
} | |
private void facebookMetadata() { | |
OpengraphProtocol graph = OpengraphProtocol.require( | |
getTitle(), | |
"article", | |
"image", | |
trip.getLink().getHref() | |
) | |
.addLocale(trip.getUser().getLocale()) | |
.addSitename(PageTitle.defaults()) | |
.addArticleAuthor(trip.getUser().getName()) | |
.addArticlePublishedTime(trip.getCreateDate()) | |
.addArticleModifiedTime(trip.getUpdateDate()); | |
trip.getTags().stream().forEach((tag) -> { | |
graph.addArticleTag( | |
tag.getLookup().getDescription() | |
); | |
}); | |
addPageMetadata(graph); | |
} | |
private void twitterMetadata() { | |
addPageMetadata(TwitterCardsMarkup.require( | |
TwitterCardsMarkup.CardTypes.SUMMARY_WITH_LARGE_IMAGE | |
.getDescription(), | |
getTitle(), | |
getDescription() | |
) | |
.addUrl(trip.getLink().getHref()) | |
.addImage("image") | |
); | |
} | |
public void googlePlusMetadata() { | |
addPageMetadata(GooglePlusSnippet.require( | |
trip.getName(), | |
getDescription(), | |
trip.getLink().getHref() | |
) | |
.addImage("image") | |
.addDatePublished(trip.getCreateDate()) | |
.addAuthor(trip.getUser().getName()) | |
); | |
} | |
@Override | |
public void buildPageMetadata() { | |
htmlMetadata(); | |
facebookMetadata(); | |
twitterMetadata(); | |
googlePlusMetadata(); | |
} | |
@Override | |
public String buildTitle() { | |
return PageTitle.evaluate(trip.getName()); | |
} | |
@Override | |
public void modify() { | |
trip.makePublic(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pamarin.rest.service.page; | |
import com.pamarin.api.model.page.PageData; | |
import com.pamarin.api.model.page.impl.TripPageContent; | |
/** | |
* @author jittagornp | |
*/ | |
public interface TripPageService { | |
PageData<TripPageContent> findByUsernameAndTripname( | |
String username, | |
String tripname, | |
boolean requireMetadata | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pamarin.rest.service.page.impl; | |
import com.pamarin.api.domain.impl.TripPostPageResource; | |
import com.pamarin.api.model.entity.Trip; | |
import com.pamarin.api.model.entity.User; | |
import com.pamarin.api.model.page.PageData; | |
import com.pamarin.api.model.page.impl.TripPageContent; | |
import com.pamarin.rest.service.Transactional; | |
import com.pamarin.rest.service.TripPostService; | |
import com.pamarin.rest.service.TripService; | |
import com.pamarin.rest.service.UserService; | |
import com.pamarin.rest.service.page.TripPageService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.data.domain.PageRequest; | |
import org.springframework.stereotype.Service; | |
import org.springframework.transaction.TransactionStatus; | |
/** | |
* @author jittagornp | |
*/ | |
@Service | |
public class TripPageServiceImpl extends Transactional implements TripPageService { | |
@Autowired | |
private UserService userService; | |
@Autowired | |
private TripService service; | |
@Autowired | |
private TripPostService postService; | |
private TripPageContent content(String username, String tripname) { | |
User user = userService.findByUsername(username); | |
Trip trip = service.findByTripnameAndUser( | |
tripname, | |
user | |
); | |
return new TripPageContent() | |
.setTrip(trip) | |
.setPostPage( | |
new TripPostPageResource( | |
postService.findByTripId( | |
trip.getId(), | |
new PageRequest(0, 5) | |
) | |
) | |
); | |
} | |
@Override | |
public PageData<TripPageContent> findByUsernameAndTripname( | |
String username, | |
String tripname, | |
boolean requireMetadata | |
) { | |
return new PageData<TripPageContent>() | |
.setContent( | |
getTransaction().execute((TransactionStatus status) -> { | |
try { | |
return content(username, tripname); | |
} catch (RuntimeException ex) { | |
status.setRollbackOnly(); | |
throw ex; | |
} | |
}) | |
) | |
.requireMedata(requireMetadata) | |
.modify(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment