Skip to content

Instantly share code, notes, and snippets.

@michaelbrazell
Created June 11, 2015 14:12
Show Gist options
  • Save michaelbrazell/c7f35563dc91b167a6a1 to your computer and use it in GitHub Desktop.
Save michaelbrazell/c7f35563dc91b167a6a1 to your computer and use it in GitHub Desktop.
package com.citytechinc.cq.bootcamp.components.content;
import groovy.transform.Field;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.jcr.RepositoryException;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import com.citytechinc.aem.bedrock.core.components.AbstractComponent;
import com.citytechinc.cq.component.annotations.Component;
import com.citytechinc.cq.component.annotations.DialogField;
import com.citytechinc.cq.component.annotations.Tab;
import com.citytechinc.cq.component.annotations.widgets.MultiField;
import com.citytechinc.cq.component.annotations.widgets.TextArea;
import com.citytechinc.cq.component.annotations.widgets.TextField;
import com.citytechinc.cq.component.annotations.widgets.RichTextEditor;
// import com.citytechinc.cq.component.annotations.widgets.rte.Lists;
import com.day.cq.commons.jcr.JcrConstants;
// import com.day.cq.wcm.foundation.List;
// import com.google.common.base.Optional;
import com.citytechinc.cq.component.annotations.widgets.Html5SmartImage;
@Component(value = "Recipe", tabs={@Tab(title="Details"), @Tab(title="Image")})
public class Recipe extends AbstractComponent {
@DialogField(fieldLabel = "Recipe Title", tab = 1, fieldDescription = "Provide the title", ranking = 1)
@TextField
public String getTitle() {
return get("title", StringUtils.EMPTY);
}
// Calendar
public String getDate() {
Calendar posted = get(JcrConstants.JCR_LASTMODIFIED, Calendar.class).get();
return new SimpleDateFormat("MM-dd-yy").format(posted.getTime());
}
// Author area
private static final String PROFILE_FAMILY_NAME_JCR_PATH = "profile/familyName";
private static final String PROFILE_GIVEN_NAME_JCR_PATH = "profile/givenName";
public String getUser() {
String lastModifiedBy = get(JcrConstants.JCR_LAST_MODIFIED_BY, String.class).get();
UserManager userManager = getResource().adaptTo(UserManager.class);
try {
Authorizable author = userManager.getAuthorizable(lastModifiedBy);
String authorName = StringUtils.EMPTY;
if (author.hasProperty(PROFILE_GIVEN_NAME_JCR_PATH)) {
authorName += author.getProperty(PROFILE_GIVEN_NAME_JCR_PATH)[0].getString() + " ";
}
if (author.hasProperty(PROFILE_FAMILY_NAME_JCR_PATH)) {
authorName += author.getProperty(PROFILE_FAMILY_NAME_JCR_PATH)[0].getString();
}
return authorName;
} catch (RepositoryException e) {
throw new RuntimeException(e);
}
}
@DialogField(fieldLabel = "Recipe Description", tab = 1, fieldDescription = "Provide the recipe description", ranking = 2)
@RichTextEditor
public String getDescription() {
return get("description", StringUtils.EMPTY);
}
@DialogField(fieldLabel = "Recipe Ingredients", tab = 1, fieldDescription = "Provide the Ingredients as a list", ranking = 3)
@MultiField
@TextField
public java.util.List<String> getIngredients() {
return getAsList("ingredients", String.class);
}
@DialogField(fieldLabel = "Recipe Steps", tab = 1, fieldDescription = "Provide the Ingredients as a list", ranking = 3)
@MultiField
@TextArea
public java.util.List<String> getSteps() {
return getAsList("steps", String.class);
}
@DialogField(fieldLabel = "Image", tab = 2, ranking = 1)
@Html5SmartImage(fileReferenceParameter ="image/fileReference", fileNameParameter = "image/fileName", allowUpload = false)
public String getRecipeImage() {
return getImageSource().or(StringUtils.EMPTY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment