Created
November 13, 2014 17:59
-
-
Save yupadhyay/4799370542604c44eecf to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import javax.jcr.RepositoryException; | |
import org.apache.felix.scr.annotations.Activate; | |
import org.apache.felix.scr.annotations.Component; | |
import org.apache.felix.scr.annotations.Deactivate; | |
import org.apache.felix.scr.annotations.Reference; | |
import org.apache.felix.scr.annotations.Service; | |
import org.apache.sling.api.resource.LoginException; | |
import org.apache.sling.api.resource.NonExistingResource; | |
import org.apache.sling.api.resource.Resource; | |
import org.apache.sling.api.resource.ResourceResolver; | |
import org.apache.sling.api.resource.ResourceResolverFactory; | |
import org.osgi.service.component.ComponentContext; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.day.cq.statistics.StatisticsService; | |
/** | |
* | |
* @author Yogesh Upadhyay | |
* | |
*/ | |
@Component | |
@Service | |
public class CustomPageImpressionImpl implements CustomImpressionService { | |
private final Logger log = LoggerFactory.getLogger(getClass()); | |
private static final String STATISTICS_PATH = "/pages"; | |
@Reference | |
private StatisticsService statisticsService; | |
@Reference | |
private ResourceResolverFactory resourceResolverFactory; | |
private ResourceResolver resourceResolver; | |
private String statisticsPath; | |
/** | |
* Record Impression Method | |
* It essentially create Impression Entry and add through OOTB service | |
*/ | |
@Override | |
public void recordImpression(String resourcePath, String date, long count) { | |
Resource resource; | |
ResourceResolver resourceResolver = null; | |
try { | |
resourceResolver = getAdminResourceResolver(); | |
resource = resourceResolver.resolve(resourcePath); | |
if(!(resource instanceof NonExistingResource)){ | |
CustomImpressionEntry customImpressionEntry = new CustomImpressionEntry(statisticsPath, resource.getPath(), date, count); | |
statisticsService.addEntry(customImpressionEntry); | |
} | |
} catch (LoginException e) { | |
log.error(e.getMessage()); | |
e.printStackTrace(); | |
} catch (RepositoryException e) { | |
log.error(e.getMessage()); | |
e.printStackTrace(); | |
} finally{ | |
closeResourceResolver(resourceResolver); | |
} | |
} | |
@Override | |
public void recordImpression(Resource resource, String date, long count) { | |
if(null!=resource){ | |
recordImpression(resource.getPath(), date,count); | |
}else{ | |
log.error("Resource Provided is Null "); | |
} | |
} | |
@Override | |
public void recordImpression(Resource resource, Date date, long count) { | |
recordImpression(resource, getFormattedDateForImpression(date),count); | |
} | |
@Override | |
public void recordImpression(Resource resource, Date date) { | |
recordImpression(resource, getFormattedDateForImpression(date),1); | |
} | |
@Override | |
public void recordImpression(String resourcePath, String date) { | |
recordImpression(resourcePath, date,1); | |
} | |
private synchronized ResourceResolver getAdminResourceResolver() throws LoginException{ | |
return resourceResolverFactory.getAdministrativeResourceResolver(null); | |
} | |
private synchronized void closeResourceResolver(ResourceResolver resourceResolver){ | |
if(null!=resourceResolver && resourceResolver.isLive()){ | |
resourceResolver.close(); | |
} | |
} | |
public String getFormattedDateForImpression(Date date){ | |
if(date!=null){ | |
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); | |
return simpleDateFormat.format(date); | |
} | |
return null; | |
} | |
@Activate | |
protected void activate(ComponentContext ctx) { | |
statisticsPath = statisticsService.getPath() + STATISTICS_PATH; | |
} | |
@Deactivate | |
protected void deactivate(ComponentContext ctx) { | |
if (resourceResolver != null && resourceResolver.isLive()) { | |
resourceResolver.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment