Working on removing portal filtering code NIHVIVO-2320 and moving processRdfForm2.jsp to java

This commit is contained in:
briancaruso 2011-04-06 15:41:52 +00:00
parent 8cc8247513
commit 835ffa3481
10 changed files with 607 additions and 906 deletions

View file

@ -5,6 +5,7 @@ package edu.cornell.mannlib.vitro.webapp.dao.filtering.filters;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import net.sf.jga.algorithms.Sort;
@ -18,6 +19,7 @@ import org.joda.time.DateTime;
import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.beans.Portal;
import edu.cornell.mannlib.vitro.webapp.beans.Tab;
import edu.cornell.mannlib.vitro.webapp.utils.FlagMathUtils;
@ -35,25 +37,6 @@ public class FiltersForTabs {
DateTime now = new DateTime();
UnaryFunctor<Individual,Boolean> entFilter = getTimeFilter(tab, now);
UnaryFunctor<Individual,Boolean> tabPortalFilter = getPortalFilter(tab);
if( isFlag1Filtering &&
tabPortalFilter != null &&
portalThatTabIsIn != null &&
portalThatTabIsIn.isFlag1Filtering() )
entFilter = AdaptorFunctors.and( entFilter, tabPortalFilter );
String flag2set = tab.getFlag2Set();
if( flag2set != null && flag2set.trim().length() > 0 ){
String[] flags = flag2set.split(",");
if( "OMIT".equalsIgnoreCase(tab.getFlag2Mode()) ){
UnaryFunctor<Individual,Boolean> negate = LogicalFunctors.unaryNegate( getCollegeFilter(flags));
entFilter = AdaptorFunctors.and( entFilter, negate);
} else {
entFilter = AdaptorFunctors.and( entFilter, getCollegeFilter(flags));
}
}
/* need more?
entFilter = AdaptorFunctors.and( getSomeFilter(tab), entFilter);
@ -69,7 +52,7 @@ public class FiltersForTabs {
UnaryFunctor<Individual,Boolean> out = null;
if( tab.getDayLimit() == 0){
out = VitroFilterUtils.getSunsetWindowFilter( now.toDate() ).getIndividualFilter();
out = getSunsetWindowFilter( now.toDate() ).getIndividualFilter();
} else if( tab.getDayLimit() > 0 ) {
out = new UnaryFunctor<Individual,Boolean>(){
@ -108,45 +91,6 @@ public class FiltersForTabs {
return out;
}
/**
* Filter the entities based on the specifications of the tab.
*/
protected static UnaryFunctor<Individual,Boolean> getPortalFilter(Tab tab){
ApplicationBean appBean = new ApplicationBean();
if( tab.getPortalId() == appBean.getSharedPortalFlagNumeric()){
return VitroFilterUtils.getCalsPortalFilter().getIndividualFilter();
//} else if (tab.getPortalId() == appBean.getAllPortalFlagNumeric()){
} else if (tab.getPortalId() == 65535){
return VitroFilterUtils.t;
}else {
return VitroFilterUtils.getFlag1ExclusiveFilter( FlagMathUtils.portalId2Numeric( tab.getPortalId() ) );
}
}
public static UnaryFunctor<Individual,Boolean>getCollegeFilter(final String[] flags){
return new UnaryFunctor<Individual,Boolean> (){
final String [] accetableColleges = flags;
public Boolean fn(Individual individual) {
String flags = individual.getFlag2Set() ;
if( flags == null || flags.trim().length() ==0 )
return Boolean.FALSE;
String[] collegesForInd = flags.split(",");
for( String accetable : accetableColleges){
for( String forInd : collegesForInd){
if( accetable.equalsIgnoreCase(forInd)) {
return Boolean.TRUE;
}
}
}
return Boolean.FALSE;
}
};
}
/**
* Create a filter to pass only entities with labels that start with the given letter.
@ -247,4 +191,75 @@ public class FiltersForTabs {
}
}
}
/** this filter accepts only objects which have sunset dates of the given date or
* earlier and sunset dates after the given date. sunrise <= givenDate < sunset.
*
* It is no longer in general use. It is only used by FitlersForTabs.
* @param givenDate - if null, use current date.
* */
@SuppressWarnings("unchecked")
public static VitroFilters getSunsetWindowFilter(final Date givenDate ){
UnaryFunctor<Individual,Boolean> fn =
new UnaryFunctor<Individual, Boolean>(){
Date given = givenDate;
//@Override
public Boolean fn(Individual arg) {
if( arg == null) return true;
return checkSunriseSunset(givenDate, arg.getSunrise(), arg.getSunset());
}
public String toString(){ return "Individual time window filter " + given; };
};
UnaryFunctor<ObjectPropertyStatement,Boolean> objPropfn =
new UnaryFunctor<ObjectPropertyStatement, Boolean>(){
Date given = givenDate;
//@Override
public Boolean fn(ObjectPropertyStatement arg) {
if( arg == null) return true;
if( checkSunriseSunset(givenDate, arg.getSunrise(), arg.getSunset()) == false)
return false;
if( arg.getObject() != null ) {
Individual obj = arg.getObject();
if( checkSunriseSunset(givenDate, obj.getSunrise(), obj.getSunset()) == false)
return false;
}
if( arg.getSubject() != null ){
Individual sub = arg.getSubject();
if( checkSunriseSunset(givenDate, sub.getSunrise(), sub.getSunset()) == false )
return false;
}
return true;
}
public String toString(){ return "ObjectPropertyStatement time window filter " + given; };
};
//we need these casts because of javac
VitroFiltersImpl vfilter = VitroFilterUtils.getNoOpFilter();
vfilter.setIndividualFilter( fn );
vfilter.setObjectPropertyStatementFilter(objPropfn);
return vfilter;
}
private static boolean checkSunriseSunset( Date now, Date sunrise, Date sunset){
if( sunrise == null && sunset == null )
return true;
DateTime nowDt = (now!=null?new DateTime(now):new DateTime());
DateTime sunriseDt = (sunrise != null ? new DateTime(sunrise): nowDt.minusDays(356));
DateTime sunsetDt = (sunset != null ? new DateTime(sunset) : nowDt.plusDays(356));
if( ( nowDt.isBefore( sunsetDt ) )
&&
( nowDt.isAfter( sunriseDt )
|| nowDt.toDateMidnight().isEqual( sunriseDt.toDateMidnight()))
)
return true;
else
return false;
}
}

View file

@ -2,7 +2,6 @@
package edu.cornell.mannlib.vitro.webapp.dao.filtering.filters;
import java.util.ArrayList;
import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
@ -12,23 +11,15 @@ import java.util.List;
import net.sf.jga.fn.UnaryFunctor;
import net.sf.jga.fn.adaptor.AdaptorFunctors;
import net.sf.jga.fn.adaptor.ChainUnary;
import net.sf.jga.fn.logical.All;
import net.sf.jga.fn.logical.UnaryNegate;
import net.sf.jga.fn.property.GetProperty;
import net.sf.jga.fn.string.Match;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.beans.BaseResourceBean.RoleLevel;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.flags.PortalFlag;
import edu.cornell.mannlib.vitro.webapp.utils.FlagMathUtils;
/**
* Static methods to help create commonly used filters.
*
@ -46,78 +37,7 @@ public class VitroFilterUtils {
*/
public static VitroFilters getDisplayFilterByRoleLevel(RoleLevel role, WebappDaoFactory wdf){
return new HiddenFromDisplayBelowRoleLevelFilter(role, wdf);
}
/** this filter accepts only objects which have sunset dates of the given date or
* earlier and sunset dates after the given date. sunrise <= givenDate < sunset.
*
* It is no longer in general use. It is only used by FitlersForTabs.
* @param givenDate - if null, use current date.
* */
@SuppressWarnings("unchecked")
public static VitroFilters getSunsetWindowFilter(final Date givenDate ){
UnaryFunctor<Individual,Boolean> fn =
new UnaryFunctor<Individual, Boolean>(){
Date given = givenDate;
//@Override
public Boolean fn(Individual arg) {
if( arg == null) return true;
return checkSunriseSunset(givenDate, arg.getSunrise(), arg.getSunset());
}
public String toString(){ return "Individual time window filter " + given; };
};
UnaryFunctor<ObjectPropertyStatement,Boolean> objPropfn =
new UnaryFunctor<ObjectPropertyStatement, Boolean>(){
Date given = givenDate;
//@Override
public Boolean fn(ObjectPropertyStatement arg) {
if( arg == null) return true;
if( checkSunriseSunset(givenDate, arg.getSunrise(), arg.getSunset()) == false)
return false;
if( arg.getObject() != null ) {
Individual obj = arg.getObject();
if( checkSunriseSunset(givenDate, obj.getSunrise(), obj.getSunset()) == false)
return false;
}
if( arg.getSubject() != null ){
Individual sub = arg.getSubject();
if( checkSunriseSunset(givenDate, sub.getSunrise(), sub.getSunset()) == false )
return false;
}
return true;
}
public String toString(){ return "ObjectPropertyStatement time window filter " + given; };
};
//we need these casts because of javac
VitroFiltersImpl vfilter = getNoOpFilter();
vfilter.setIndividualFilter( fn );
vfilter.setObjectPropertyStatementFilter(objPropfn);
return vfilter;
}
private static boolean checkSunriseSunset( Date now, Date sunrise, Date sunset){
if( sunrise == null && sunset == null )
return true;
DateTime nowDt = (now!=null?new DateTime(now):new DateTime());
DateTime sunriseDt = (sunrise != null ? new DateTime(sunrise): nowDt.minusDays(356));
DateTime sunsetDt = (sunset != null ? new DateTime(sunset) : nowDt.plusDays(356));
if( ( nowDt.isBefore( sunsetDt ) )
&&
( nowDt.isAfter( sunriseDt )
|| nowDt.toDateMidnight().isEqual( sunriseDt.toDateMidnight()))
)
return true;
else
return false;
}
}
/** Gets a VitroFilters that permits all objects */
protected static VitroFiltersImpl getNoOpFilter(){
@ -131,215 +51,7 @@ public class VitroFilterUtils {
UnaryFunctor<Individual,Boolean> onlyNamesThatStartWithG =
new ChainUnary<Individual,String,Boolean>(startsWithG,getName);
return getNoOpFilter().setIndividualFilter(onlyNamesThatStartWithG);
}
/**
* Gets a set of VitroFilters for a given portal flag.
* This method may return null. */
public static VitroFilters getFilterFromPortalFlag(PortalFlag flag){
if( ! flag.isFilteringActive() )
return getNoOpFilter();
ApplicationBean appBean = new ApplicationBean();
if( flag.getFlag1Numeric() == appBean.getSharedPortalFlagNumeric()){
return VitroFilterUtils.getCalsPortalFilter();
} else if (flag.getFlag1Numeric() == appBean.getAllPortalFlagNumeric() ){
return VitroFilterUtils.getAllPortalFilter();
}
//common, single portal configuration with only flag1 active
if(PortalFlag.SHOW_ALL_PORTALS != flag.getFlag1DisplayStatus() &&
flag.flag1Active &&
! flag.flag2Active && ! flag.flag3Active)
{
return new VitroFiltersImpl().setIndividualFilter(
getFlag1ExclusiveFilter( flag.getFlag1Numeric() ));
}
ArrayList<UnaryFunctor<Individual,Boolean>> functors =
new ArrayList<UnaryFunctor<Individual,Boolean>> (3);
UnaryFunctor<Individual,Boolean> flag1Fn = null;
if( PortalFlag.SHOW_ALL_PORTALS == flag.getFlag1DisplayStatus() ){
functors.add( t );
}else if( flag.flag1Active && flag.getFlag1Numeric() > 0 ){
if( flag.isFlag1Exclusive() )
flag1Fn =getFlag1ExclusiveFilter( flag.getFlag1Numeric() );
else
flag1Fn = getFlag1OmitFilter( flag.getFlag1Numeric() );
functors.add( flag1Fn );
}
UnaryFunctor<Individual,Boolean> flag2Fn = null;
if( flag.flag2Active && flag.getFlag2Numeric() > 0 ){
if( flag.isFlag2Exclusive() )
flag2Fn =getFlag2ExclusiveFilter( flag.getFlag2Numeric() );
else
flag2Fn = getFlag2OmitFilter( flag.getFlag2Numeric() );
functors.add( flag2Fn );
}
UnaryFunctor<Individual,Boolean> flag3Fn = null;
if( flag.flag3Active && flag.getFlag3Numeric() > 0 ){
if( flag.isFlag2Exclusive() )
flag3Fn =getFlag3ExclusiveFilter( flag.getFlag3Numeric() );
else
flag3Fn = getFlag3OmitFilter( flag.getFlag3Numeric() );
functors.add( flag3Fn );
}
VitroFiltersImpl vf = new VitroFiltersImpl();
if( functors.size() == 1 )
vf.setIndividualFilter(functors.get(0));
else if( functors.size() > 0 ){
//this is a filter where all of the conditions in the collection are true
UnaryFunctor<Individual,Boolean> indFilter
= new All<Individual>( functors );
vf.setIndividualFilter(indFilter);
}
return vf;
}
/**
* All portal no longer does any portal flag based
* filtering. If a Individual needs to be hidden it
* could be hidden using the hiddenFromDisplayBelowRoleLevel setting,
* although this would re-introduce the performance hit of a-box filtering .
* @return returns null
*/
public static VitroFilters getAllPortalFilter(){
// VitroFiltersImpl vf = new VitroFiltersImpl();
// vf.setIndividualFilter(ALL_PORTAL_FILTER);
// return vf;
return null;
}
private static UnaryFunctor<Individual,Boolean> ALL_PORTAL_FILTER =
new UnaryFunctor<Individual,Boolean>(){
public Boolean fn(Individual ind){
if (ind == null ) return false;
return ind.getFlag1Numeric() != 0 ;
}
public String toString(){ return "allow any non-zero flag1 Individuals";}
};
/**
* get a filter that will only pass entities that are checked into
* the portal with the given id. All non-Individual objects will be
* passed.
*
* @return
*/
@SuppressWarnings("serial")
public static UnaryFunctor<Individual,Boolean> getFlag1ExclusiveFilter(final long portalIdNumeric) {
return
new UnaryFunctor<Individual,Boolean>(){
int portalMask = (int)portalIdNumeric;
@Override
public Boolean fn(Individual arg) {
if( arg == null ) return Boolean.FALSE;
return arg.doesFlag1Match( portalMask );
}
public String toString(){ return "Flag1Exclusive Filter on " +
"individual.portal & " + portalMask;
}
};
}
protected static UnaryFunctor<Individual, Boolean> getFlag1OmitFilter(
int flag1Numeric) {
return new UnaryNegate<Individual>( getFlag1ExclusiveFilter( flag1Numeric ));
}
@SuppressWarnings("serial")
protected static UnaryFunctor<Individual,Boolean> getFlag3ExclusiveFilter(long flag3Numeric) {
final long _numeric = flag3Numeric;
return
new UnaryFunctor<Individual,Boolean>(){
long mask = _numeric ;
@Override
public Boolean fn(Individual arg) {
if( arg == null ) return Boolean.FALSE;
int entFlag3 = arg.getFlag3Numeric();
boolean isEntInPortal = (mask & entFlag3) != 0;
return new Boolean( isEntInPortal );
}
};
}
protected static UnaryFunctor<Individual, Boolean> getFlag3OmitFilter(
int flag3Numeric) {
return new UnaryNegate<Individual>( getFlag3ExclusiveFilter( flag3Numeric ));
}
@SuppressWarnings("serial")
protected static UnaryFunctor<Individual,Boolean> getFlag2ExclusiveFilter(long flag2Numeric) {
final long _numeric = flag2Numeric;
return
new UnaryFunctor<Individual,Boolean>(){
long mask = _numeric;
@Override
public Boolean fn(Individual arg) {
if( arg == null ) return Boolean.FALSE;
int entflag2 = ((Individual)arg).getFlag2Numeric();
boolean isEntInPortal = (mask & entflag2) != 0;
return new Boolean( isEntInPortal );
}
};
}
protected static UnaryFunctor<Individual, Boolean> getFlag2OmitFilter(
int flag2Numeric) {
return new UnaryNegate<Individual>( getFlag2ExclusiveFilter( flag2Numeric ));
}
/**
* Get a filter that will emulate the 'all portal' behavior.
* Notice that the 'all portal' does not have all of the
* entities that exist in the system. This fillter will
* pass anything checked into portals 2,3,4,5 aka
* life sci, env sci, lang grant, applied soc sci.
*
* That's the bit mask 001111
*
* All non-Individual objects will be passed.
* @return
*/
public static VitroFilters getCalsPortalFilter() {
final long portalIdNumeric =
FlagMathUtils.portalId2Numeric( 2 ) +
FlagMathUtils.portalId2Numeric( 3 ) +
FlagMathUtils.portalId2Numeric( 4 ) +
FlagMathUtils.portalId2Numeric( 5 );
UnaryFunctor<Individual,Boolean> indFn
= new UnaryFunctor<Individual,Boolean>(){
long portalMask = portalIdNumeric;
@Override
public Boolean fn(Individual arg) {
if( arg == null ) return Boolean.FALSE;
int entPortalNum = ((Individual)arg).getFlag1Numeric();
boolean isEntInPortal = (portalMask & entPortalNum) != 0;
return new Boolean( isEntInPortal );
}
public String toString(){ return "CalsPortal Filter, is individual in " +
"portal 2,3,4 or 5?";
}
};
VitroFiltersImpl vf = new VitroFiltersImpl();
vf.setIndividualFilter(indFn);
return vf;
}
}
public static UnaryFunctor<Individual,String> FirstLetterOfIndividuals(){
return new UnaryFunctor<Individual,String>(){

View file

@ -3,6 +3,7 @@ package edu.cornell.mannlib.vitro.webapp.dao.jena;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
@ -39,7 +40,6 @@ public class VClassGroupCache{
private static final Log log = LogFactory.getLog(VClassGroupCache.class);
private static final String ATTRIBUTE_NAME = "VClassGroupCache";
private static final String REBUILD_EVERY_PORTAL = "Rebuild every portal.";
private static final boolean ORDER_BY_DISPLAYRANK = true;
private static final boolean INCLUDE_UNINSTANTIATED = true;
@ -51,25 +51,28 @@ public class VClassGroupCache{
public static VClassGroupCache getVClassGroupCache(ServletContext sc){
return (VClassGroupCache) sc.getAttribute(ATTRIBUTE_NAME);
}
/** This is the cache of VClassGroups. It is a portal id to list of VClassGroups */
private final ConcurrentHashMap<Integer, List<VClassGroup>> _groupListMap;
private final ConcurrentLinkedQueue<String> _rebuildQueue;
private final RebuildGroupCacheThread _cacheRebuildThread;
/** Indicates that a rebuild of the VCLassGroupCache is needed. */
private Boolean _rebuildRequested;
/** This is the cache of VClassGroups. It is a list of VClassGroups.
* If this is null then the cache is not built. */
private List<VClassGroup> _groupList;
private final RebuildGroupCacheThread _cacheRebuildThread;
private final ServletContext context;
private VClassGroupCache(ServletContext context) {
this.context = context;
this._groupListMap = new ConcurrentHashMap<Integer, List<VClassGroup>>();
this._rebuildQueue = new ConcurrentLinkedQueue<String>();
this._groupList = null;
if( AbortStartup.isStartupAborted(context)){
_cacheRebuildThread = null;
return;
}
VClassGroupCacheChangeListener bccl = new VClassGroupCacheChangeListener(this);
VClassGroupCacheChangeListener bccl = new VClassGroupCacheChangeListener();
ModelContext.registerListenerForChanges(context, bccl);
//
// ModelContext.getJenaOntModel(context).register(bccl);
@ -79,19 +82,20 @@ public class VClassGroupCache{
// ModelContext.getBaseOntModelSelector(context).getABoxModel().register(bccl);
// ModelContext.getInferenceOntModelSelector(context).getABoxModel().register(bccl);
_rebuildQueue.add(REBUILD_EVERY_PORTAL);
_rebuildRequested = true;
_cacheRebuildThread = new RebuildGroupCacheThread(this);
_cacheRebuildThread.setDaemon(true);
_cacheRebuildThread.start();
_cacheRebuildThread.informOfQueueChange();
}
public void clearGroupCache(){
_groupListMap.clear();
public void clearGroupCache(){
_groupList.clear();
}
/**
* May return null.
* @deprecated use getGroup(String vlCassGroupURI) instead.
*/
public VClassGroup getGroup( int portalId, String vClassGroupURI ){
if( vClassGroupURI == null || vClassGroupURI.isEmpty() )
@ -104,124 +108,61 @@ public class VClassGroupCache{
return null;
}
public VClassGroup getGroup( String vClassGroupURI ){
if( vClassGroupURI == null || vClassGroupURI.isEmpty() )
return null;
List<VClassGroup> cgList = getGroups();
for( VClassGroup cg : cgList ){
if( vClassGroupURI.equals( cg.getURI()))
return cg;
}
return null;
}
/**
* @deprecated use getGroups() instead.
*/
public List<VClassGroup> getGroups( int portalId ){
return getGroups(getVCGDao(),portalId );
}
//bdc34: this has been de-portaled.
if( _groupList == null )
return rebuildCache();
else
return _groupList;
}
private List<VClassGroup> getGroups( VClassGroupDao vcgDao, int portalId) {
return getGroups( vcgDao, portalId, INCLUDE_INDIVIDUAL_COUNT);
}
private List<VClassGroup> getGroups( VClassGroupDao vcgDao , int portalId, boolean includeIndividualCount ){
List<VClassGroup> groupList = _groupListMap.get(portalId);
if( groupList == null ){
log.debug("needed to build vclassGroups for portal " + portalId);
// Get all classgroups, each populated with a list of their member vclasses
List<VClassGroup> groups =
vcgDao.getPublicGroupsWithVClasses(ORDER_BY_DISPLAYRANK, INCLUDE_UNINSTANTIATED, includeIndividualCount);
// remove classes that have been configured to be hidden from search results
vcgDao.removeClassesHiddenFromSearch(groups);
// now cull out the groups with no populated classes
//vcgDao.removeUnpopulatedGroups(groups);
return groups;
} else {
return groupList;
}
}
public List<VClassGroup> getGroups( ){
if( _groupList == null )
return rebuildCache();
else
return _groupList;
}
private void requestCacheUpdate(String portalUri){
log.debug("requesting update for portal " + portalUri);
_rebuildQueue.add(portalUri);
private synchronized void requestCacheUpdate(){
log.debug("requesting update");
_rebuildRequested = true;
_cacheRebuildThread.informOfQueueChange();
}
}
protected synchronized void refreshGroupCache() {
long start = System.currentTimeMillis();
try{
boolean rebuildAll = false;
HashSet<String> portalURIsToRebuild = new HashSet<String>();
String portalUri;
while ( null != (portalUri = _rebuildQueue.poll()) ){
if( portalUri.equals(REBUILD_EVERY_PORTAL)){
rebuildAll = true;
_rebuildQueue.clear();
break;
}else{
portalURIsToRebuild.add(portalUri);
}
}
WebappDaoFactory wdFactory = (WebappDaoFactory)context.getAttribute("webappDaoFactory");
if( wdFactory == null ){
log.error("Unable to rebuild cache: could not get 'webappDaoFactory' from Servletcontext");
return;
}
Collection<Portal> portals;
if( rebuildAll ){
portals = wdFactory.getPortalDao().getAllPortals();
} else {
portals = new LinkedList<Portal>();
for( String uri : portalURIsToRebuild){
Portal p =wdFactory.getPortalDao().getPortalByURI(uri);
if( p!= null)
portals.add(wdFactory.getPortalDao().getPortalByURI(uri));
}
}
for(Portal portal : portals){
rebuildCacheForPortal(portal,wdFactory);
}
log.info("rebuilt ClassGroup cache in " + (System.currentTimeMillis() - start) + " msec");
}catch (Exception ex){
log.error("could not rebuild cache", ex);
}
}
protected synchronized void rebuildCacheForPortalUri(String uri){
WebappDaoFactory wdFactory = (WebappDaoFactory)context.getAttribute("webappDaoFactory");
if( wdFactory == null ){
log.error("Unable to rebuild cache: could not get 'webappDaoFactory' from Servletcontext");
return;
}
Portal portal = wdFactory.getPortalDao().getPortalByURI(uri);
rebuildCacheForPortal(portal,wdFactory);
}
protected synchronized void rebuildCacheForPortal(Portal portal, WebappDaoFactory wdFactory){
VitroFilters vFilters = null;
protected synchronized List<VClassGroup> rebuildCache(){
long start = System.currentTimeMillis();
try{
WebappDaoFactory wdFactory = (WebappDaoFactory)context.getAttribute("webappDaoFactory");
if( wdFactory == null ){
log.error("Unable to rebuild cache: could not get 'webappDaoFactory' from Servletcontext");
return Collections.emptyList();
}
VitroFilters vFilters = null;
vFilters = VitroFilterUtils.getDisplayFilterByRoleLevel(RoleLevel.PUBLIC, wdFactory);
WebappDaoFactory filteringDaoFactory ;
if( vFilters !=null ){
filteringDaoFactory = new WebappDaoFactoryFiltering(wdFactory,vFilters);
}else{
filteringDaoFactory = wdFactory;
}
boolean singlePortalApplication = wdFactory.getPortalDao().getAllPortals().size() == 1;
if ( singlePortalApplication ) {
if ( vFilters == null )
vFilters = VitroFilterUtils.getDisplayFilterByRoleLevel(RoleLevel.PUBLIC, wdFactory);
} else if ( portal.isFlag1Filtering() ){
PortalFlag pflag = new PortalFlag(portal.getPortalId());
if( vFilters == null)
vFilters = VitroFilterUtils.getFilterFromPortalFlag(pflag);
else
vFilters = vFilters.and( VitroFilterUtils.getFilterFromPortalFlag(pflag));
}
WebappDaoFactory filteringDaoFactory ;
if( vFilters !=null ){
filteringDaoFactory = new WebappDaoFactoryFiltering(wdFactory,vFilters);
}else{
filteringDaoFactory = wdFactory;
}
_groupListMap.remove(portal.getPortalId());
if ( !singlePortalApplication ) {
_groupListMap.put(portal.getPortalId(),
getGroups(filteringDaoFactory.getVClassGroupDao(),portal.getPortalId()));
} else {
List<VClassGroup> unfilteredGroups = getGroups(wdFactory.getVClassGroupDao(), portal.getPortalId(), INCLUDE_INDIVIDUAL_COUNT);
List<VClassGroup> filteredGroups = getGroups(filteringDaoFactory.getVClassGroupDao(),portal.getPortalId(), !INCLUDE_INDIVIDUAL_COUNT);
_groupListMap.put(portal.getPortalId(), removeFilteredOutGroupsAndClasses(unfilteredGroups, filteredGroups));
// BJL23: You may be wondering, why this extra method?
// Can't we just use the filtering DAO?
// Yes, but using the filtered DAO involves an expensive method
@ -231,8 +172,34 @@ public class VClassGroupCache{
// this work when portal filtering is involved, but we can
// short-circuit it when we have a single portal by using
// the filtering DAO only to filter groups and classes,
// and the unfiltered DAO to get the counts.
}
// and the unfiltered DAO to get the counts.
List<VClassGroup> unfilteredGroups = getGroups(wdFactory.getVClassGroupDao(), INCLUDE_INDIVIDUAL_COUNT);
List<VClassGroup> filteredGroups = getGroups(filteringDaoFactory.getVClassGroupDao(), !INCLUDE_INDIVIDUAL_COUNT);
List<VClassGroup> groups = removeFilteredOutGroupsAndClasses(unfilteredGroups, filteredGroups);
// Remove classes that have been configured to be hidden from search results.
filteringDaoFactory.getVClassGroupDao().removeClassesHiddenFromSearch(groups);
_groupList = groups;
_rebuildRequested = false;
log.info("rebuilt ClassGroup cache in " + (System.currentTimeMillis() - start) + " msec");
return _groupList;
}catch (Exception ex){
log.error("could not rebuild cache", ex);
return Collections.emptyList();
}
}
private List<VClassGroup> getGroups( VClassGroupDao vcgDao , boolean includeIndividualCount ){
// Get all classgroups, each populated with a list of their member vclasses
List<VClassGroup> groups =
vcgDao.getPublicGroupsWithVClasses(ORDER_BY_DISPLAYRANK, INCLUDE_UNINSTANTIATED, includeIndividualCount);
// remove classes that have been configured to be hidden from search results
vcgDao.removeClassesHiddenFromSearch(groups);
return groups;
}
private List<VClassGroup> removeFilteredOutGroupsAndClasses(List<VClassGroup> unfilteredGroups, List<VClassGroup> filteredGroups) {
@ -285,12 +252,7 @@ public class VClassGroupCache{
}
/* ****************** Jena Model Change Listener***************************** */
private class VClassGroupCacheChangeListener extends StatementListener {
private VClassGroupCache cache = null;
public VClassGroupCacheChangeListener(VClassGroupCache cache){
this.cache=cache;
}
private class VClassGroupCacheChangeListener extends StatementListener {
public void addedStatement(Statement stmt) {
checkAndDoUpdate(stmt);
}
@ -306,11 +268,9 @@ public class VClassGroupCache{
log.debug("predicate: " + stmt.getPredicate().getURI());
}
if( RDF.type.getURI().equals( stmt.getPredicate().getURI()) ){
requestCacheUpdate(REBUILD_EVERY_PORTAL);
} else if( VitroVocabulary.PORTAL_FLAG1FILTERING.equals( stmt.getPredicate().getURI())){
requestCacheUpdate(stmt.getSubject().getURI());
requestCacheUpdate();
} else if( VitroVocabulary.IN_CLASSGROUP.equals( stmt.getPredicate().getURI() )){
requestCacheUpdate(REBUILD_EVERY_PORTAL);
requestCacheUpdate();
}
}
}
@ -329,7 +289,7 @@ public class VClassGroupCache{
while (!die) {
int delay;
if (_rebuildQueue.isEmpty()) {
if (_rebuildRequested == Boolean.FALSE) {
log.debug("rebuildGroupCacheThread.run() -- queue empty, sleep");
delay = 1000 * 60;
} else if ((System.currentTimeMillis() - queueChangeMillis.get()) < 200) {
@ -337,7 +297,7 @@ public class VClassGroupCache{
delay = 200;
} else {
log.debug("rebuildGroupCacheThread.run() -- refreshGroupCache()");
cache.refreshGroupCache();
cache.rebuildCache();
delay = 0;
}

View file

@ -0,0 +1,262 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.controller;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditConfiguration;
import edu.cornell.mannlib.vitro.webapp.web.MiscWebUtils;
/**
* This servlet is intended to handle all requests to create a form for use
* by the N3 editing system. It will examine the request parameters, determine
* which form to use, execute a EditConfiguration setup, and evaluate the
* view indicated by the EditConfiguration.
*
* Do not add code to this class to achieve some behavior in a
* form. Try adding the behavior logic to the code that generates the
* EditConfiguration for the form.
*/
public class EditRequestDispatch extends FreemarkerHttpServlet {
private static final long serialVersionUID = 1L;
public static Log log = LogFactory.getLog(EditRequestDispatch.class);
final String DEFAULT_OBJ_FORM = "defaultObjPropForm.jsp";
final String DEFAULT_ERROR_FORM = "error.jsp";
final String DEFAULT_ADD_INDIVIDUAL = "defaultAddMissingIndividualForm.jsp";
@Override
protected ResponseValues processRequest(VitroRequest vreq) {
WebappDaoFactory wdf = vreq.getWebappDaoFactory();
//get edit key.
//The edit key links submissions to EditConfiguration objects in the session.
HttpSession session = vreq.getSession();
String editKey =
(EditConfiguration.getEditKey(vreq) == null)
? EditConfiguration.newEditKey(session)
: EditConfiguration.getEditKey(vreq);
vreq.setAttribute("editKey", editKey);
//set title to Edit to maintain functionality from 1.1.1 and avoid updates to Selenium tests
vreq.setAttribute("title","Edit");
String subjectUri = vreq.getParameter("subjectUri");
String predicateUri = vreq.getParameter("predicateUri");
String formParam = vreq.getParameter("editform");
String command = vreq.getParameter("cmd");
//check some error conditions
if (formParam == null || "".equals(formParam)) {
if ((predicateUri == null || predicateUri.trim().length() == 0)) {
return doHelp(vreq, "No form was specified, both predicateUri and"
+ " editform are empty. One of these is required"
+ " by editRequestDispatch to choose a form.");
}
if (subjectUri == null || subjectUri.trim().length() == 0){
return doHelp(vreq, "subjectUri was empty. If no editForm is specified," +
" it is required by EditRequestDispatch.");
}
}
vreq.setAttribute("subjectUri", subjectUri);
vreq.setAttribute("subjectUriJson", MiscWebUtils.escape(subjectUri));
if (predicateUri != null) {
vreq.setAttribute("predicateUri", predicateUri);
vreq.setAttribute("predicateUriJson", MiscWebUtils.escape(predicateUri));
}
if (formParam != null && formParam.length() > 0) {
vreq.setAttribute("editForm", formParam);
} else {
formParam = null;
}
//bdc34: typeOfNew is used by some forms like defaultAddMissingindividuaForm
//it should be moved out of this code and into the configuration for those forms
String typeOfNew = vreq.getParameter("typeOfNew");
if( typeOfNew != null )
vreq.setAttribute("typeOfNew", typeOfNew);
vreq.setAttribute("urlPatternToReturnTo", vreq
.getParameter("urlPattern") == null ? "/entity" : vreq
.getParameter("urlPattern"));
log.debug("setting urlPatternToReturnTo as "
+ vreq.getAttribute("urlPatternToReturnTo"));
/* since we have the URIs lets put the individuals in the request */
if( subjectUri != null ){
Individual subject = wdf.getIndividualDao().getIndividualByURI(subjectUri);
if( subject != null )
vreq.setAttribute("subject", subject);
}
String objectUri = vreq.getParameter("objectUri");
if (objectUri != null) {
vreq.setAttribute("objectUri", objectUri);
vreq.setAttribute("objectUriJson", MiscWebUtils.escape(objectUri));
}
boolean isEditOfExistingStmt = false;
if (objectUri != null) {
Individual object = wdf.getIndividualDao().getIndividualByURI(objectUri);
if (object != null) {
vreq.setAttribute("object", object);
isEditOfExistingStmt = true;
}
}
// Keep track of what form we are using so it can be returned to after a failed validation
// I'd like to get this from the request but sometimes that doesn't work well, internal forwards etc.
String url = "/edit/editRequestDispatch.jsp";
vreq.setAttribute("formUrl", url + "?" + vreq.getQueryString());
//this are only used by the old jsp forms
vreq.setAttribute("preForm", "/edit/formPrefix.jsp");
vreq.setAttribute("postForm", "/edit/formSuffix.jsp");
if ("delete".equals(command)) {
// %><jsp:forward page="/edit/forms/propDelete.jsp"/><%
return null;
}
//Certain predicates may be annotated to change the behavior of the edit
//link. Check for this annotation and, if present, simply redirect
//to the normal individual display for the object URI instead of bringing
//up an editing form.
//Note that we do not want this behavior for the delete link (handled above).
// This might be done in the custom form jsp for publicaitons already.
// so maybe this logic shouldn't be here?
if ( isEditOfExistingStmt && (wdf.getObjectPropertyDao().skipEditForm(predicateUri)) ) {
log.debug("redirecting to object for predicate " + predicateUri);
// %><c:redirect url="/individual">
// <c:param name="uri" value="${param.objectUri}"/>
// <c:param name="relatedSubjectUri" value="${param.subjectUri}"/>
// <c:param name="relatingPredicateUri" value="${param.predicateUri}"/>
// </c:redirect>
// <%
return null;
}
String form = DEFAULT_OBJ_FORM;
// *** handle the case where the form is specified as a request parameter ***
if( predicateUri == null && ( formParam != null && !formParam.isEmpty()) ){
//case where a form was passed as a http parameter
form = formParam;
vreq.setAttribute("form", form);
//followed by <jsp:foward page="/edit/forms/${form}"/>
return null;
}
// *** handle the case where the form is decided by the predicate parameter ***
//check to see if we have a predicate and if it has a custom form
//if it has a custom form associated with it then use that form,
//otherwise use the default object property form
String customForm = null;
ObjectProperty objectProp = wdf.getObjectPropertyDao().getObjectPropertyByURI(predicateUri);
if( objectProp != null ){
vreq.setAttribute("predicate", objectProp);
customForm = objectProp.getCustomEntryForm();
}
// Forward to create new is part of the default object property form
// it should be handled in that form's EditConfiguration, not here.
// The code that sets up the EditConfiguration should decide on
// different configurations and templates to use based on isForwardToCreateNew.
if( isFowardToCreateNew(vreq, objectProp, command)){
return handleForwardToCreateNew(vreq, command, objectProp, isEditOfExistingStmt);
}
//Offer create new and select from existing are ignored if there is a custom form
if (customForm != null && customForm.length() > 0) {
//if there is a custom form on the predicate, use that
form = objectProp.getCustomEntryForm();
} else {
//if it is nothing special, then use the default object property form
form = DEFAULT_OBJ_FORM ;
}
vreq.setAttribute("form", form);
// Now here we can no longer forward to a JSP.
// Somehow we need to be able to specify some java code that generates the
// EditConfiguration and the do the freemarker template merge.
return null;
}
/*
Forward to create new is part of the default object property form
it should be handled in that form's EditConfiguration, not here.
The code that sets up the EditConfiguration should decide on
different configurations and templates to use based on isForwardToCreateNew.
*/
boolean isFowardToCreateNew(VitroRequest vreq, ObjectProperty objectProp, String command){
//Offer create new and select from existing are ignored if there is a custom form
if( objectProp != null && objectProp.getCustomEntryForm() != null && !objectProp.getCustomEntryForm().isEmpty()){
return false;
} else {
boolean isForwardToCreateNew =
( objectProp != null && objectProp.getOfferCreateNewOption() && objectProp.getSelectFromExisting() == false)
|| ( objectProp != null && objectProp.getOfferCreateNewOption() && "create".equals(command));
return isForwardToCreateNew;
}
}
ResponseValues handleForwardToCreateNew(VitroRequest vreq, String command, ObjectProperty objectProp, boolean isEditOfExistingStmt){
vreq.setAttribute("isForwardToCreateNew", new Boolean(true));
//If a objectProperty is both provideSelect and offerCreateNewOption
// and a user goes to a defaultObjectProperty.jsp form then the user is
// offered the option to create a new Individual and replace the
// object in the existing objectPropertyStatement with this new individual.
boolean isReplaceWithNew =
isEditOfExistingStmt && "create".equals(command)
&& objectProp != null && objectProp.getOfferCreateNewOption() == true;
// If an objectProperty is selectFromExisitng==false and offerCreateNewOption == true
// the we want to forward to the create new form but edit the existing object
// of the objPropStmt.
boolean isForwardToCreateButEdit =
isEditOfExistingStmt && objectProp != null
&& objectProp.getOfferCreateNewOption() == true
&& objectProp.getSelectFromExisting() == false
&& ! "create".equals(command);
//bdc34: maybe when doing a create new, the custom form should be on the class, not the property?
String form;
if( isReplaceWithNew ){
vreq.setAttribute("isReplaceWithNew", new Boolean(true));
form = DEFAULT_ADD_INDIVIDUAL;
}else if( isForwardToCreateButEdit ){
vreq.setAttribute("isForwardToCreateButEdit", new Boolean(true));
form = DEFAULT_ADD_INDIVIDUAL;
}else {
form = DEFAULT_ADD_INDIVIDUAL;
}
//forward to form?
return null;
}
private ResponseValues doHelp(VitroRequest vreq, String message){
//output some sort of help message for the developers.
return null;
}
}

View file

@ -21,19 +21,19 @@ import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.Field;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.shared.Lock;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.IndividualImpl;
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.TemplateProcessingHelper.TemplateProcessingException;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ForwardResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.dao.InsertException;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.dao.jena.DependentResourceDeleteJena;
@ -42,135 +42,128 @@ import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditConfiguration;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditN3Generator;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditN3Utils;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditSubmission;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.Field;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.ModelChangePreprocessor;
/**
* This servlet will process EditConfigurations with query parameters
* to perform an edit.
*
* The general steps involved are:
* get edit configuration
* get edit submission
*
*
*
*/
public class ProcessRdfForm extends VitroHttpServlet{
public class ProcessRdfForm extends FreemarkerHttpServlet{
//bdc34: How will we change this so it is not a jsp?
public static final String NO_EDITCONFIG_FOUND_JSP = "/edit/messages/noEditConfigFound.jsp" ;
//bdc34: this is likely to become a servlet instead of a jsp
// you can get a servlet from the context.
public static final String POST_EDIT_CLEANUP_JSP = "postEditCleanUp.jsp";
private Log log = LogFactory.getLog(ProcessRdfForm.class);
//we should get this from the application, ConfigurationProperties?
static String defaultUriPrefix = "http://vivo.library.cornell.edu/ns/0.1#individual";
private Log log = LogFactory.getLog(ProcessRdfForm.class);
/* entity to return to may be a variable */
List<String> entToReturnTo = new ArrayList<String>(1);
//bdc34: this is likely to become a servlet instead of a jsp.
// You can get a reference to the servlet from the context.
// this will need to be converted from a jsp to something else
public static final String POST_EDIT_CLEANUP_JSP = "postEditCleanUp.jsp";
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException{
doGet(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException{
ServletException, IOException{
VitroRequest vreq = new VitroRequest(request);
RequestDispatcher requestDispatcher;
//get the EditConfiguration
EditConfiguration editConfiguration = getEditConfiguration(request);
if(editConfiguration == null){
requestDispatcher = request.getRequestDispatcher(NO_EDITCONFIG_FOUND_JSP);
requestDispatcher.forward(request, response);
EditConfiguration configuration = getEditConfiguration(request);
if(configuration == null){
doEditConfigNotFound( vreq, response);
return;
}
//get the EditSubmission
EditSubmission submission = new EditSubmission(vreq.getParameterMap(), editConfiguration);
EditSubmission submission = new EditSubmission(vreq.getParameterMap(), configuration);
EditSubmission.putEditSubmissionInSession(request.getSession(), submission);
boolean hasErrors = processValidationErrors(vreq, editConfiguration, submission, response);
boolean hasErrors = processValidationErrors(vreq, configuration, submission, response);
if( hasErrors)
return; //processValidationErrors() already forwarded to redisplay the form with validation messages
OntModel queryModel = editConfiguration.getQueryModelSelector().getModel(request, getServletContext());
// get the model to write to here in case a preprocessor has switched the write layer
OntModel writeModel = editConfiguration.getWriteModelSelector().getModel(request,getServletContext());
// get the models to work with in case the write model and query model are not the defaults
OntModel queryModel = configuration.getQueryModelSelector().getModel(request, getServletContext());
OntModel writeModel = configuration.getWriteModelSelector().getModel(request,getServletContext());
AdditionsAndRetractions changes;
if(editConfiguration.isUpdate()){
changes = editExistingResource(editConfiguration, submission);
if(configuration.isUpdate()){
changes = editExistingResource(configuration, submission);
}else{
changes = createNewResource(editConfiguration, submission);
changes = createNewResource(configuration, submission);
}
applyChanges(changes, editConfiguration, request, queryModel, writeModel);
changes = getMinimalChanges( changes );
/* what about entity to return to? */
if( configuration.isUseDependentResourceDelete() )
changes = addDependentDeletes(changes, queryModel);
requestDispatcher = vreq.getRequestDispatcher(POST_EDIT_CLEANUP_JSP);
requestDispatcher.forward(vreq, response);
preprocessModels(changes, configuration, vreq);
applyChangesToWriteModel(changes, queryModel, writeModel, EditN3Utils.getEditorUri(vreq) );
//Here we are trying to get the entity to return to URL,
//Maybe this should be in POST_EDIT_CLEANUP?
if( configuration.getEntityToReturnTo() != null ){
request.setAttribute("entityToReturnTo", substitueForURL( configuration, submission));
}
doPostEdit(vreq,response);
}
private void applyChanges(AdditionsAndRetractions changes, EditConfiguration editConfiguration, HttpServletRequest request, OntModel queryModel, OntModel writeModel) {
/**
* Execute any modelChangePreprocessors in the editConfiguration;
*/
protected void preprocessModels(AdditionsAndRetractions changes, EditConfiguration editConfiguration, VitroRequest request){
List<ModelChangePreprocessor> modelChangePreprocessors = editConfiguration.getModelChangePreprocessors();
if ( modelChangePreprocessors != null ) {
for ( ModelChangePreprocessor pp : modelChangePreprocessors ) {
//these work by side effect
pp.preprocess( changes.getRetractions(), changes.getAdditions(), request );
}
}
}
protected AdditionsAndRetractions getMinimalChanges( AdditionsAndRetractions changes ){
//make a model with all the assertions and a model with all the
//retractions, do a diff on those and then only add those to the jenaOntModel
Model allPossibleAssertions = ModelFactory.createDefaultModel();
Model allPossibleRetractions = ModelFactory.createDefaultModel();
for( Model model : changes.getAdditions() ) {
allPossibleAssertions.add( model );
}
for( Model model : changes.getRetractions() ){
allPossibleRetractions.add( model );
}
//find the minimal change set
Model actualAssertions = allPossibleAssertions.difference( allPossibleRetractions );
Model actualRetractions = allPossibleRetractions.difference( allPossibleAssertions );
//retractions, do a diff on those and then only add those to the jenaOntModel
Model allPossibleAssertions = changes.getAdditions();
Model allPossibleRetractions = changes.getRetractions();
//find the minimal change set
Model assertions = allPossibleAssertions.difference( allPossibleRetractions );
Model retractions = allPossibleRetractions.difference( allPossibleAssertions );
return new AdditionsAndRetractions(assertions,retractions);
}
protected AdditionsAndRetractions addDependentDeletes( AdditionsAndRetractions changes, Model queryModel){
//Add retractions for dependent resource delete if that is configured and
//if there are any dependent resources.
if( editConfiguration.isUseDependentResourceDelete() ){
Model depResRetractions =
DependentResourceDeleteJena
.getDependentResourceDeleteForChange(actualAssertions,actualRetractions,queryModel);
actualRetractions.add( depResRetractions );
}
//execute any modelChangePreprocessors
List<ModelChangePreprocessor> modelChangePreprocessors = editConfiguration.getModelChangePreprocessors();
if ( modelChangePreprocessors != null ) {
for ( ModelChangePreprocessor pp : modelChangePreprocessors ) {
pp.preprocess( actualRetractions, actualAssertions, request );
}
}
//side effect: modify the write model with the changes
String editorUri = EditN3Utils.getEditorUri(new VitroRequest(request));
//if there are any dependent resources.
Model depResRetractions =
DependentResourceDeleteJena
.getDependentResourceDeleteForChange(changes.getAdditions(),changes.getRetractions(),queryModel);
changes.getRetractions().add(depResRetractions);
return changes;
}
protected void applyChangesToWriteModel(AdditionsAndRetractions changes, OntModel queryModel, OntModel writeModel, String editorUri) {
//side effect: modify the write model with the changes
Lock lock = null;
try{
lock = writeModel.getLock();
lock.enterCriticalSection(Lock.WRITE);
writeModel.getBaseModel().notifyEvent(new EditEvent(editorUri,true));
writeModel.add( actualAssertions );
writeModel.remove( actualRetractions );
writeModel.add( changes.getAdditions() );
writeModel.remove( changes.getRetractions() );
}catch(Throwable t){
log.error("error adding edit change n3required model to in memory model \n"+ t.getMessage() );
}finally{
writeModel.getBaseModel().notifyEvent(new EditEvent(editorUri,false));
lock.leaveCriticalSection();
}
//Here we are trying to get the entity to return to URL set up correctly.
//The problme is that subInURI will add < and > around URIs for the N3 syntax.
//for the URL to return to, replace < and > from URI additions.
//This should happen somewhere else...
if( entToReturnTo.size() >= 1 && entToReturnTo.get(0) != null){
request.setAttribute("entityToReturnTo",
entToReturnTo.get(0).trim().replaceAll("<","").replaceAll(">",""));
}
}
}
private EditConfiguration getEditConfiguration(HttpServletRequest request) {
@ -202,7 +195,6 @@ public class ProcessRdfForm extends VitroHttpServlet{
if(log.isDebugEnabled()) {
Utilities.logRequiredOpt("substituted in URIs off from ",n3Required,n3Optional);
}
entToReturnTo = n3Subber.subInUris(submission.getUrisFromForm(), entToReturnTo);
//sub in literals from form
n3Required = n3Subber.subInLiterals(submission.getLiteralsFromForm(), n3Required);
@ -217,7 +209,6 @@ public class ProcessRdfForm extends VitroHttpServlet{
if(log.isDebugEnabled()) {
Utilities.logRequiredOpt("substituted in URIs from scope ",n3Required,n3Optional);
}
entToReturnTo = n3Subber.subInUris(editConfiguration.getUrisInScope(), entToReturnTo);
n3Required = n3Subber.subInLiterals( editConfiguration.getLiteralsInScope(), n3Required);
n3Optional = n3Subber.subInLiterals( editConfiguration.getLiteralsInScope(), n3Optional);
@ -258,18 +249,13 @@ public class ProcessRdfForm extends VitroHttpServlet{
model.read(reader, "", "N3");
optionalNewModels.add(model);
}catch(Throwable t){
//if an optional N3 block fails to parse it will be ignored
// errorMessages.add("error processing optional n3 string \n"+
// t.getMessage() + '\n' +
// "n3: \n" + n3);
//if an optional N3 block fails to parse it will be ignored
//this is what is meant by optional.
}
}
requiredAssertions.addAll( optionalNewModels );
AdditionsAndRetractions delta = new AdditionsAndRetractions();
delta.setAdditions(requiredAssertions);
delta.setRetractions(Collections.<Model>emptyList());
return delta;
return new AdditionsAndRetractions(requiredAssertions, Collections.<Model>emptyList());
}
@SuppressWarnings("static-access")
@ -279,24 +265,18 @@ public class ProcessRdfForm extends VitroHttpServlet{
Map<String, List<String>> fieldRetractions = Utilities.fieldsToRetractionMap(editConfiguration.getFields());
EditN3Generator n3Subber = editConfiguration.getN3Generator();
if(editConfiguration.getEntityToReturnTo() != null){
entToReturnTo.add(" " + editConfiguration.getEntityToReturnTo() + " ");
}
/* ********** URIs and Literals on Form/Parameters *********** */
fieldAssertions = n3Subber.substituteIntoValues(submission.getUrisFromForm(), submission.getLiteralsFromForm(), fieldAssertions);
if(log.isDebugEnabled()) {
Utilities.logAddRetract("substituted in literals from form",fieldAssertions,fieldRetractions);
}
entToReturnTo = n3Subber.subInUris(submission.getUrisFromForm(),entToReturnTo);
}
/* ****************** URIs and Literals in Scope ************** */
fieldAssertions = n3Subber.substituteIntoValues(editConfiguration.getUrisInScope(), editConfiguration.getLiteralsInScope(), fieldAssertions );
fieldRetractions = n3Subber.substituteIntoValues(editConfiguration.getUrisInScope(), editConfiguration.getLiteralsInScope(), fieldRetractions);
if(log.isDebugEnabled()) {
Utilities.logAddRetract("substituted in URIs and Literals from scope",fieldAssertions,fieldRetractions);
}
entToReturnTo = n3Subber.subInUris(editConfiguration.getUrisInScope(),entToReturnTo);
}
//do edits ever need new resources? (YES)
/* Map<String,String> varToNewResource = newToUriMap(editConfiguration.getNewResources(),wdf);
@ -348,18 +328,19 @@ public class ProcessRdfForm extends VitroHttpServlet{
}
}
// requiredAssertions = requiredFieldAssertions;
// requiredRetractions = requiredFieldRetractions;
// optionalAssertions = Collections.emptyList();
AdditionsAndRetractions delta = new AdditionsAndRetractions();
delta.setAdditions(requiredFieldAssertions);
delta.setRetractions(requiredFieldRetractions);
return delta;
// throw new Error("need to be implemented by Deepak");
return new AdditionsAndRetractions(requiredFieldAssertions, requiredFieldRetractions);
}
private void doEditConfigNotFound(VitroRequest request, HttpServletResponse response) {
HashMap<String,Object>map = new HashMap<String,Object>();
map.put("message", "No editing configuration found, cannot process edit.");
ResponseValues values = new TemplateResponseValues("message.ftl", map);
try {
doResponse(request,response,values);
} catch (TemplateProcessingException e) {
log.error("Could not process template for doEditConfigNotFound()",e);
}
}
private boolean processValidationErrors(VitroRequest vreq,
EditConfiguration editConfiguration, EditSubmission submission,
@ -370,8 +351,8 @@ public class ProcessRdfForm extends VitroHttpServlet{
if(errors != null && !errors.isEmpty()){
String form = editConfiguration.getFormUrl();
vreq.setAttribute("formUrl", form);
vreq.setAttribute("view", vreq.getParameter("view"));
vreq.setAttribute("view", vreq.getParameter("view"));
RequestDispatcher requestDispatcher = vreq.getRequestDispatcher(editConfiguration.getFormUrl());
requestDispatcher.forward(vreq, response);
return true;
@ -379,10 +360,12 @@ public class ProcessRdfForm extends VitroHttpServlet{
return false;
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException{
doGet(request, response);
}
private void doPostEdit(VitroRequest vreq, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher requestDispatcher = vreq.getRequestDispatcher(POST_EDIT_CLEANUP_JSP);
requestDispatcher.forward(vreq, response);
}
public static class Utilities {
@ -474,103 +457,69 @@ public class ProcessRdfForm extends VitroHttpServlet{
if( required != null ) log.debug( "required: " + required.toString() );
if( optional != null ) log.debug( "optional: " + optional.toString() );
return true;
}
}
/* What are the posibilities and what do they mean?
field is a Uri:
orgValue formValue
null null Optional object property, maybe a un-filled out checkbox or radio button.
non-null null There was an object property and it was unset on the form
null non-null There was an objProp that was not set and is now set.
non-null non-null If they are the same then there was no edit, if the differ then form field was changed
field is a Literal:
orgValue formValue
null null Optional value that was not set.
non-null null Optional value that was unset on form
null non-null Optional value that was unset but was set on form
non-null non-null If same, there was no edit, if different, there was a change to the form field.
What about checkboxes?
*/
private boolean hasFieldChanged(String fieldName, EditConfiguration editConfig, EditSubmission submission) {
String orgValue = editConfig.getUrisInScope().get(fieldName);
String newValue = submission.getUrisFromForm().get(fieldName);
// see possibilities list in comments just above
if (orgValue == null && newValue != null) {
log.debug("Note: Setting previously null object property for field '"+fieldName+"' to new value ["+newValue+"]");
return true;
}
if( orgValue != null && newValue != null){
if( orgValue.equals(newValue))
return false;
else
return true;
}
//This does NOT use the semantics of the literal's Datatype or the language.
Literal orgLit = editConfig.getLiteralsInScope().get(fieldName);
Literal newLit = submission.getLiteralsFromForm().get(fieldName);
if( orgLit != null ) {
orgValue = orgLit.getValue().toString();
}
if( newLit != null ) {
newValue = newLit.getValue().toString();
}
// added for links, where linkDisplayRank will frequently come in null
if (orgValue == null && newValue != null) {
return true;
}
if( orgValue != null && newValue != null ){
if( orgValue.equals(newValue)) {
return false;
}
else {
return true;
}
}
//value wasn't set originally because the field is optional
return false;
}
}
/**
* This is intended to substitute vars from the EditConfiguration and
* EditSubmission into the URL to return to.
*/
protected String substitueForURL(EditConfiguration configuration, EditSubmission submission){
private void dump(String name, Object fff){
XStream xstream = new XStream(new DomDriver());
System.out.println( "*******************************************************************" );
System.out.println( name );
System.out.println(xstream.toXML( fff ));
}
}
private String subInValues(String in /* what else is needed? */){
return in;
}
private class AdditionsAndRetractions {
List<Model> additions;
List<Model> retractions;
List<String> entToReturnTo = new ArrayList<String>(1);
entToReturnTo.add(configuration.getEntityToReturnTo());
EditN3Generator n3Subber = configuration.getN3Generator();
// Substitute in URIs from the submission
entToReturnTo = n3Subber.subInUris(submission.getUrisFromForm(), entToReturnTo);
public List<Model> getAdditions() {
// Substitute in URIs from the scope of the EditConfiguration
entToReturnTo = n3Subber.subInUris(configuration.getUrisInScope(), entToReturnTo);
//The problem is that subInURI will add < and > around URIs for the N3 syntax.
//for the URL to return to, replace < and > from URI additions.
return entToReturnTo.get(0).trim().replaceAll("<","").replaceAll(">","");
}
/**
* This is a data structure to allow a method to return
* a pair of Model objects for additions and retractions.
*/
protected class AdditionsAndRetractions {
Model additions;
Model retractions;
public AdditionsAndRetractions(List<Model>adds, List<Model>retractions){
Model allAdds = ModelFactory.createDefaultModel();
Model allRetractions = ModelFactory.createDefaultModel();
for( Model model : adds ) {
allAdds.add( model );
}
for( Model model : retractions ){
allRetractions.add( model );
}
this.setAdditions(allAdds);
this.setRetractions(allRetractions);
}
public AdditionsAndRetractions(Model add, Model retract){
this.additions = add;
this.retractions = retract;
}
public Model getAdditions() {
return additions;
}
public void setAdditions(List<Model> additions) {
public void setAdditions(Model additions) {
this.additions = additions;
}
public List<Model> getRetractions() {
public Model getRetractions() {
return retractions;
}
public void setRetractions(List<Model> retractions) {
public void setRetractions(Model retractions) {
this.retractions = retractions;
}
}

View file

@ -176,6 +176,7 @@ public class VitroRequestPrep implements Filter {
filters = getFiltersFromContextFilterFactory((HttpServletRequest)request, wdf);
/* bdc34:to be removed in vivo 1.3
if( wdf.getApplicationDao().isFlag1Active() && (portalFlag != null) ){
VitroFilters portalFilter =
VitroFilterUtils.getFilterFromPortalFlag(portalFlag);
@ -184,6 +185,7 @@ public class VitroRequestPrep implements Filter {
else
filters = portalFilter;
}
*/
if( filters != null ){
log.debug("Wrapping WebappDaoFactory in portal filters");

View file

@ -18,10 +18,10 @@ import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatementImpl;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.IndividualImpl;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatementImpl;
import edu.cornell.mannlib.vitro.webapp.dao.filtering.BaseFiltering;
import edu.cornell.mannlib.vitro.webapp.dao.filtering.tabFactory.TabEntityFactoryFiltering;
import edu.cornell.mannlib.vitro.webapp.utils.FlagMathUtils;
public class VitroFiltersFactoryTest {
@ -35,7 +35,7 @@ public class VitroFiltersFactoryTest {
DateTime easyDate = new org.joda.time.DateTime(2005,1,1,0,0,0,0); //2005-01-01
Date givenDate = easyDate.toDate();
VitroFilters vf = VitroFilterUtils.getSunsetWindowFilter(givenDate);
VitroFilters vf = FiltersForTabs.getSunsetWindowFilter(givenDate);
Assert.assertNotNull(vf);
checkFilterForNull(vf);
@ -87,7 +87,7 @@ public class VitroFiltersFactoryTest {
DateTime easyDate = new org.joda.time.DateTime(2005,1,1,0,0,0,0); //2005-01-01
Date givenDate = easyDate.toDate();
VitroFilters vf = VitroFilterUtils.getSunsetWindowFilter(givenDate);
VitroFilters vf = FiltersForTabs.getSunsetWindowFilter(givenDate);
Assert.assertNotNull(vf);
checkFilterForNull(vf);
@ -196,39 +196,7 @@ public class VitroFiltersFactoryTest {
private int portalId2Numeric(long i) {
return (int)FlagMathUtils.portalId2Numeric( i);
}
@Test
public void testCalsFilter(){
VitroFilters vf = VitroFilterUtils.getCalsPortalFilter();
IndividualImpl ind = new IndividualImpl();
ind.setFlag1Numeric(0);
Assert.assertTrue(vf.getIndividualFilter().fn(ind) == REJECT);
ind.setFlag1Numeric( portalId2Numeric(1) );
Assert.assertTrue(vf.getIndividualFilter().fn(ind) == REJECT);
ind.setFlag1Numeric( portalId2Numeric(2));
Assert.assertTrue(vf.getIndividualFilter().fn(ind) == ACCEPT);
ind.setFlag1Numeric( portalId2Numeric(3));
Assert.assertTrue(vf.getIndividualFilter().fn(ind) == ACCEPT);
ind.setFlag1Numeric( portalId2Numeric(4));
Assert.assertTrue(vf.getIndividualFilter().fn(ind) == ACCEPT);
ind.setFlag1Numeric( portalId2Numeric(5));
Assert.assertTrue(vf.getIndividualFilter().fn(ind) == ACCEPT);
ind.setFlag1Numeric( portalId2Numeric(6));
Assert.assertTrue(vf.getIndividualFilter().fn(ind) == REJECT);
ind.setFlag1Numeric( portalId2Numeric(7));
Assert.assertTrue(vf.getIndividualFilter().fn(ind) == REJECT);
ind.setFlag1Numeric( portalId2Numeric(2) + portalId2Numeric(1));
Assert.assertTrue(vf.getIndividualFilter().fn(ind) == ACCEPT);
}
}
public void checkFilterForNull(VitroFilters vf){
Assert.assertNotNull("filter was null", vf);

View file

@ -1,85 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.dao.filtering.filters;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.IndividualImpl;
import edu.cornell.mannlib.vitro.webapp.flags.PortalFlag;
import edu.cornell.mannlib.vitro.webapp.utils.FlagMathUtils;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class WebappDaoFilteringPortalTest {
String[][] entsDef = {
// entity name flag 1 numeric
{"0", "only in portal 1", Long.toString( portalId2Numeric(1) )},
{"1", "In portal 1 and 2", Long.toString( portalId2Numeric(1) + portalId2Numeric(2) )},
{"2", "in portal 3", Long.toString( portalId2Numeric(3) )},
{"3", "checked into no portal", "0"},
{"4", "checked into portal 4", Long.toString( portalId2Numeric(4) )},
{"5", "checked into 1 and 4", Long.toString( portalId2Numeric(1) + portalId2Numeric(4) )},
{"6", "checked into 4", Long.toString( portalId2Numeric(4) )},
{"7", "checked into 5", Long.toString( portalId2Numeric(5) )},
};
List <Individual> ents = new ArrayList (10);
@Before
public void setUp() throws Exception {
//we need something that makes classes
for(String entA[] : entsDef){
Individual ent = new IndividualImpl();
ent.setName("Item " + entA[0] + ": " + entA[1]);
ent.setFlag1Numeric( Integer.parseInt(entA[2]) );
ents.add(ent);
}
}
private long portalId2Numeric(long i) {
return FlagMathUtils.portalId2Numeric( i);
}
@Test
public void testPortal(){
//Is it in portal 2 (aka numeric portal 4, in vivo 'life sci' ) ?
PortalFlag f = new PortalFlag( 2 );
VitroFilters vf = VitroFilterUtils.getFilterFromPortalFlag(f);
Assert.assertNotNull( vf );
Assert.assertFalse( vf.getIndividualFilter().fn( ents.get(0) ) );
Assert.assertTrue( vf.getIndividualFilter().fn( ents.get(1) ) );
Assert.assertFalse( vf.getIndividualFilter().fn( ents.get(2) ) );
Assert.assertFalse( vf.getIndividualFilter().fn( ents.get(3) ) );
Assert.assertFalse( vf.getIndividualFilter().fn( ents.get(4) ) );
Assert.assertFalse( vf.getIndividualFilter().fn( ents.get(5) ) );
//Is it in portal 1 (aka numeric portal 2, in vivo 'vivo portal' ) ?
f = new PortalFlag(1);
vf = VitroFilterUtils.getFilterFromPortalFlag(f);
Assert.assertNotNull( vf );
Assert.assertTrue( vf.getIndividualFilter().fn( ents.get(0) ) );
Assert.assertTrue( vf.getIndividualFilter().fn( ents.get(1) ) );
Assert.assertFalse( vf.getIndividualFilter().fn( ents.get(2) ) );
Assert.assertFalse( vf.getIndividualFilter().fn( ents.get(3) ) );
Assert.assertFalse( vf.getIndividualFilter().fn( ents.get(4) ) );
Assert.assertTrue( vf.getIndividualFilter().fn( ents.get(5) ) );
}
@Test
public void testAllPorta(){
VitroFilters vf = VitroFilterUtils.getCalsPortalFilter();
Assert.assertNotNull( vf );
Assert.assertFalse( vf.getIndividualFilter().fn( ents.get(0) ) );
Assert.assertTrue( vf.getIndividualFilter().fn( ents.get(1) ) );
Assert.assertTrue( vf.getIndividualFilter().fn( ents.get(2) ) );
Assert.assertFalse( vf.getIndividualFilter().fn( ents.get(3) ) );
Assert.assertTrue( vf.getIndividualFilter().fn( ents.get(4) ) );
Assert.assertTrue( vf.getIndividualFilter().fn( ents.get(5) ) );
Assert.assertTrue( vf.getIndividualFilter().fn( ents.get(6) ) );
Assert.assertTrue( vf.getIndividualFilter().fn( ents.get(7) ) );
}
}

View file

@ -1,83 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.utils;
import net.sf.jga.fn.UnaryFunctor;
import org.joda.time.DateTime;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.IndividualImpl;
import edu.cornell.mannlib.vitro.webapp.beans.Portal;
import edu.cornell.mannlib.vitro.webapp.beans.Tab;
import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.FiltersForTabs;
/**
* User: bdc34
* Date: Dec 13, 2007
* Time: 2:47:03 PM
*/
public class FiltersForTabsTest {
@Before
public void setup(){
}
@Test
public void testTabCollegeFiltering(){
String flags [] = {"CALS","BOAK","FUNGORK","MACAWI"};
UnaryFunctor<Individual,Boolean> testFn =
FiltersForTabs.getCollegeFilter(flags);
Assert.assertTrue ( testFn!=null);
IndividualImpl ind = new IndividualImpl();
Assert.assertTrue(testFn.fn(ind) == false);
ind.setFlag2Set("BOAK");
Assert.assertTrue( testFn.fn(ind) == true);
ind.setFlag2Set("");
Assert.assertTrue(testFn.fn(ind) == false);
ind.setFlag2Set("CALS,BOAK,FUNGORK");
Assert.assertTrue(testFn.fn(ind) == true);
ind.setFlag2Set("FINKLY,HAPTO,FOOTOP");
Assert.assertTrue(testFn.fn(ind) == false);
ind.setFlag2Set("FINKLY,HAPTO,FOOTOP,CALS");
Assert.assertTrue(testFn.fn(ind) == true);
}
@Test
public void testCollegeFilterCreation(){
Tab tab = new Tab();
tab.setFlag2Set("CALS,BOAK,FUNGORK");
tab.setPortalId(7);
UnaryFunctor<Individual,Boolean> testFn =
FiltersForTabs.getFilterForTab(tab, new Portal(), false);
Assert.assertTrue ( testFn!=null);
IndividualImpl ind = new IndividualImpl();
Assert.assertFalse( testFn.fn( ind) );
ind.setFlag2Set("CALS");
ind.setFlag1Numeric((int)FlagMathUtils.portalId2Numeric( tab.getPortalId() ));
DateTime dt = new DateTime();
ind.setSunrise(dt.minusDays(1000).toDate());
ind.setSunset(dt.plusDays(1000).toDate());
Assert.assertTrue( testFn.fn( ind));
tab.setFlag2Mode("OMIT");
testFn = FiltersForTabs.getFilterForTab(tab, new Portal(), false);
Assert.assertFalse( testFn.fn(ind));
}
}

View file

@ -230,6 +230,7 @@ public static Log log = LogFactory.getLog("edu.cornell.mannlib.vitro.webapp.jsp.
<%!
//bdc34: This isn't used anywhere, don't migrate forward to java code in 1.3
private static synchronized void setEditReferer(String editKey, String refererUrl, HttpSession session) {
if (refererUrl != null) {
Object editRefererObj = session.getAttribute("editRefererMap");