Thursday, December 25, 2014

Implementing Drag and Drop functionality in ADF Calendar

To achieve Drag and Drop functionality for activities within the adf Calendar:

- Add 'Calendar Drop Target' as a child to Calendar:

<af:calendarDropTarget dropListener="#{pageFlowScope.calendarBean.calendarDropListener}" actions="MOVE">
    <af:dataFlavor flavorClass="org.apache.myfaces.trinidad.model.RowKeySet" discriminant="DnDAcivityModel"/>
</af:calendarDropTarget>

- Add the code below in calendarDropListener

  public DnDAction calendarDropListener(DropEvent dropEvent)
  {
    TimeZone timeZone1 = TimeZone.getDefault();
    Transferable transferable = dropEvent.getTransferable();
    CalendarActivity activity =
      (CalendarActivity) transferable.getData(DataFlavor.getDataFlavor(CalendarActivity.class));

    CalendarDropSite dropSite = (CalendarDropSite) dropEvent.getDropSite();
    Date dropTargetDate = dropSite.getDate(); //Date at drop target, time is defaulted at 00:00:00 in month view.
    System.out.println("Drop Date:"+ dropTargetDate);       
    
    Date sourceStartDate = activity.getStartDate(timeZone1); // Start date of dragged activity
    Date sourceEndDate = activity.getEndDate(timeZone1); // End date of dragged activity
    System.out.println("Old start date:" + sourceStartDate);
    System.out.println("Old end date:" + sourceEndDate); 

    // Calculate new start date for dragged activity
    Date targetStartDate = null;   
    // In day and week view
    if (calendarBinding.getView().equals("day") || calendarBinding.getView().equals("week"))
    {
      // Start date will be same as target date
      targetStartDate = dropTargetDate;
    }
    else // In month view
    {
        // New start date will be target date with start date time component
        // If  start date is 12/23/2014 07:38:16 and target date is 12/24/2014 00:00:00
        // then the new start date will be 12/24/2014 07:38:16
        targetStartDate = setStartDate(sourceStartDate, dropTargetDate);
    }
    
    // Calculate new end date by adding the difference of start and end date to new start date.
    Date targetEndDate =
      setEndDate(sourceStartDate, sourceEndDate, targetStartDate); 

    System.out.println("New start Date:" + targetStartDate);
    System.out.println("New end Date:" + targetEndDate);
  
    //Update Calendar Model with new start and end dates for dragged activity
    activity.setStartDate(targetStartDate, timeZone1);
    activity.setEndDate(targetEndDate, timeZone1);
  
    OperationBinding op =  ADFUtils.findOperation("Commit");
    op.execute();

    return dropEvent.getProposedAction();
  }

  public Date setStartDate(Date sourceStartDate, Date targetDate)
  {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(sourceStartDate);
    int hour = calendar.get(Calendar.HOUR);
    int minute = calendar.get(Calendar.MINUTE);
    int second = calendar.get(Calendar.SECOND);
    calendar.setTime(targetDate);
    int year = calendar.get(Calendar.YEAR); 
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DATE);
    calendar.set(year, month, day, hour, minute, second);    
    Date newStartDate = calendar.getTime();
    return newStartDate;
  }

  public Date setEndDate(Date sourceStartDate, Date sourceEndDate,
                         Date targetStartDate)
  {
    long diff = sourceEndDate.getTime() - sourceStartDate.getTime();
    int diffDays = (int) ((diff) / (1000 * 60 * 60 * 24));
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(sourceEndDate);   
    int tHour = calendar.get(Calendar.HOUR);
    int tMinute = calendar.get(Calendar.MINUTE);
    int tSecond = calendar.get(Calendar.SECOND);
    calendar.setTime(sourceStartDate);
    int sHour = calendar.get(Calendar.HOUR);
    int sMinute = calendar.get(Calendar.MINUTE);
    int sSecond = calendar.get(Calendar.SECOND);
    calendar.setTime(targetStartDate);
    calendar.add(Calendar.DATE, diffDays);
    calendar.add(Calendar.HOUR, tHour - sHour);
    calendar.add(Calendar.MINUTE, tMinute - sMinute);
    calendar.add(Calendar.SECOND, tSecond - sSecond);
    Date newEndDate = calendar.getTime();  
    return newEndDate;
  } 


For more details on adding Drag and Drop functionality to adf Calendar refer to Oracle docs

Wednesday, December 24, 2014

Finding User Identity using Identity Governance Framework

The Identity Governance Framework (IGF) enables secure exchange of identity-related information between users and applications and service providers (Oracle docs). Below is the code snippet for finding users based on their attribute values:

import oracle.security.jps.JpsContext;
import oracle.security.jps.JpsContextFactory;
import oracle.security.jps.service.idstore.IdentityStoreService;

import oracle.igf.ids.IdentityDirectory;
import oracle.igf.ids.ResultSet;
import oracle.igf.ids.SearchFilter;
import oracle.igf.ids.SearchOptions;
import oracle.igf.ids.User;
import oracle.igf.ids.UserManager;

public class IGFExample
{
  private void findUsersFromAttributeVal(String attrVal)
  {
    IdentityDirectory ids = null;
    UserManager uMgr = null;
    try
    {
      JpsContext context =
        JpsContextFactory.getContextFactory().getContext();
      IdentityStoreService idstore =
        (IdentityStoreService) context.getServiceInstance(IdentityStoreService.class);
      ids = idstore.getIdentityStore();
      uMgr = ids.getUserManager();

      //We are trying to find users whose description or displayname contains attrValue
      SearchFilter filter =
        new SearchFilter(SearchFilter.LogicalOp.OR, new SearchFilter("description",
                                                                     SearchFilter.Operator.CONTAINS,
                                                                     attrVal),
                         new SearchFilter("displayname",
                                          SearchFilter.Operator.CONTAINS,
                                          attrVal));
      SearchOptions searchOpts = new SearchOptions();
      //Sorting the results by firstname
      searchOpts.setSortAttrs(new String[]
          { "firstname" });
      ResultSet sr = uMgr.searchUsers(filter, searchOpts);
      while (sr.hasMore())
      {
        User user = sr.getNext();
        System.out.println("User Name: " + user.getSubjectName());
        System.out.println("User ID: " + user.getId());
      }
    }
    catch (Exception exp)
    {
      exp.printStackTrace();
    }
  }
}



Thursday, December 18, 2014

Hide Validation Error Note Window on ADF Components

Following skin settings hide error window that pops up on the side of ADF compoent in case of any validation rules are violated for that component, but you still get to see the red border around the component

.AFNoteWindowConeBorder{
display: none
}
.AFNoteWindowConeBL{
display: none
}
.AFNoteWindow{
display: none
}

<< A Request >>
  Please contribute in making this world a better place to live in by adopting a pet from your nearest pet shelter (btw cats are my favourite :-) )
<< Thank You >>