Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 JBoss Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *       http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.guvnor.inbox.backend.server;
 
 import java.util.List;
 
 
 public class InboxServiceImpl
         implements InboxService {
 
     static final int MAX_RECENT_EDITED = 200;
 
     public static final String RECENT_EDITED_ID = "recentEdited";
     public static final String RECENT_VIEWED_ID = "recentViewed";
     public static final String INCOMING_ID = "incoming";
     private static final String INBOX = "inbox";
 
     @Inject
     @Named("ioStrategy")
     private IOService ioService;
 
     @Inject
     private UserServicesImpl userServices;
 
     @Inject
     @SessionScoped
     private Identity identity;
 
     @Inject
 
     public PageResponse<InboxPageRowloadInboxInboxPageRequest request ) {
         if ( request == null ) {
             throw new IllegalArgumentException"request cannot be null" );
         }
         if ( request.getPageSize() != null && request.getPageSize() < 0 ) {
             throw new IllegalArgumentException"pageSize cannot be less than zero." );
         }
 
         String inboxName = request.getInboxName();
         PageResponse<InboxPageRowresponse = new PageResponse<InboxPageRow>();
 
         List<InboxEntryentries = loadEntriesinboxName );
         Iterator<InboxEntryiterator = entries.iterator();
         List<InboxPageRowrowList = new InboxPageRowBuilder()
                 .withPageRequestrequest )
                 .withIdentity )
                 .withContentiterator )
                 .build();
 
         response = new PageResponseBuilder<InboxPageRow>()
                 .withStartRowIndexrequest.getStartRowIndex() )
                 .withTotalRowSizeentries.size() )
                 .withTotalRowSizeExact()
                 .withPageRowListrowList )
                 .withLastPage( !iterator.hasNext() )
                 .build();
 
         return response;
     }
 
     public List<InboxEntryloadEntriesfinal String inboxName ) {
         List<InboxEntryentries;
         if ( inboxName.equals ) ) {
            entries = loadRecentOpened();
        } else if ( inboxName.equals ) ) {
            entries = loadRecentEdited();
        } else {
            entries = loadIncoming();
        }
        return entries;
    }
    public List<InboxEntryloadRecentEdited() {
        return readEntries.getName(),  );
    }
    public List<InboxEntryloadRecentEditedString userName ) {
        return readEntriesuserName );
    }
    public List<InboxEntryloadRecentOpened() {
        return readEntries.getName(),  );
    }
    public List<InboxEntryloadIncoming() {
        return readEntries.getName(),  );
    }
    public List<InboxEntryloadIncomingString userName ) {
        return readEntriesuserName );
    }
    public List<InboxEntryreadEntriesString userName,
                                         String boxName ) {
        Path path = .buildPathuserNameboxName );
        if ( .existspath ) ) {
            final String xml = .readAllStringpath );
            if ( !( xml == null || xml.equals"" ) ) ) {
                return (List<InboxEntry>) getXStream().fromXMLxml );
            } else {
                return new ArrayList<InboxEntry>();
            }
        }
        return new ArrayList<InboxEntry>();
    }
    public void recordOpeningEvent( @Observes final ResourceOpenedEvent event ) {
        PortablePreconditions.checkNotNull"event"event );
        final org.uberfire.backend.vfs.Path resourcePath = event.getPath();
        recordOpeningEventresourcePath.toURI(), resourcePath.getFileName().toString() );
    }

    
Helper method to log the opening. Will remove any inbox items that have the same id.
    public synchronized void recordOpeningEventString itemPath,
                                                 String itemName ) {
        addToRecentOpeneditemPathitemName );
        List<InboxEntryunreadIncoming = removeAnyExistingitemPath,
                                                             loadIncoming() );
        writeEntriesunreadIncoming );
    }
    public void recordUserEditEvent( @Observes final ResourceUpdatedEvent event ) {
        PortablePreconditions.checkNotNull"event"event );
        final org.uberfire.backend.vfs.Path resourcePath = event.getPath();
        recordUserEditEventresourcePath.toURI(), resourcePath.getFileName().toString() );
    }

    
Helper method to note the event
    //@Override
    public synchronized void recordUserEditEventString itemPath,
                                                  String itemName ) {
        addToRecentEditeditemPathitemName );
        //deliver messages to users inboxes (ie., the edited item is the itme that the current logged in user has edited in the past, or commented on)
        addToIncomingitemPathitemName.getName(), . );
        .wakeUp();
    }

    
This should be called when the user edits or comments on an asset. Simply adds to the list...
    public void addToRecentEditedString itemPath,
                                   String note ) {
        addToInbox,
                    itemPath,
                    note,
                    .getName(),
                    .getName() );
    }
    public void addToRecentOpenedString itemPath,
                                   String note ) {
        addToInbox,
                    itemPath,
                    note,
                    .getName(),
                    .getName() );
    }
    public void addToIncomingString itemPath,
                               String note,
                               String userFrom,
                               String userName ) {
        addToInbox,
                    itemPath,
                    note,
                    userFrom,
                    userName );
    }
    private void addToInboxString boxName,
                             String itemPath,
                             String note,
                             String userFrom,
                             String userName ) {
        assert boxName.equals ) || boxName.equals ) || boxName.equals );
        List<InboxEntryentries = removeAnyExistingitemPath,
                                                      readEntriesuserNameboxName ) );
        if ( entries.size() >=  ) {
            entries.remove( 0 );
            entries.addnew InboxEntryitemPath,
                                         note,
                                         userFrom ) );
        } else {
            entries.addnew InboxEntryitemPath,
                                         note,
                                         userFrom ) );
        }
        writeEntriesboxNameentries );
    }
    private List<InboxEntryremoveAnyExistingString itemPath,
                                                List<InboxEntryinboxEntries ) {
        Iterator<InboxEntryit = inboxEntries.iterator();
        while ( it.hasNext() ) {
            InboxEntry e = it.next();
            if ( e.itemPath.equalsitemPath ) ) {
                it.remove();
                return inboxEntries;
            }
        }
        return inboxEntries;
    }
    public void writeEntriesString boxName,
                              List<InboxEntryentries ) {
        Path path = .buildPathboxName );
        //System.out.println( "writeEntries: " + path.toString() );
        String entry = getXStream().toXMLentries );
        //System.out.println( "writeEntries: " + entry );
        .writepathentry );
    }
    private XStream getXStream() {
        XStream xs = new XStream();
        xs.alias"inbox-entries"List.class );
        xs.alias"entry"InboxEntry.class );
        return xs;
    }

    
And entry in an inbox.
    public static class InboxEntry {
        public String from;
        public InboxEntry() {
        }
        public InboxEntryString itemPath,
                           String note,
                           String userFrom ) {
            this. = itemPath;
            this. = note;
            this. = System.currentTimeMillis();
            this. = userFrom;
        }
        public String itemPath;
        public String note;
        public long timestamp;
    }
New to GrepCode? Check out our FAQ X