pickitem-widget/src/main/java/org/gcube/portlets/widgets/pickitem/server/PickItemServiceImpl.java

99 lines
3.9 KiB
Java

package org.gcube.portlets.widgets.pickitem.server;
import java.util.ArrayList;
import java.util.List;
import org.gcube.portlets.widgets.pickitem.client.rpc.PickItemService;
import org.gcube.portlets.widgets.pickitem.shared.ItemBean;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.util.ManagementUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.OrderByComparatorFactoryUtil;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
@SuppressWarnings("serial")
public class PickItemServiceImpl extends RemoteServiceServlet implements PickItemService {
private static final Logger _log = LoggerFactory.getLogger(PickItemServiceImpl.class);
/**
*
* @return true if you're running into the portal, false if in development
*/
private boolean isWithinPortal() {
try {
UserLocalServiceUtil.getService();
return true;
}
catch (com.liferay.portal.kernel.bean.BeanLocatorException ex) {
_log.trace("Development Mode ON");
return false;
}
}
//TODO: Questo metodo lunedì va modificato per fargli cercare non tutti gli utenti ma dipdende dallo scope i VRE Members oppure l'unione se è root, e poi vanno gestiti i gruppi stessa cosa.
//anzi dovresti farti passare come parametro la VRE dove è stato postato il post allora sarebbe semplicissimo, niente union non serve
@Override
public ArrayList<ItemBean> searchEntities(String keyword) {
ArrayList<ItemBean> toReturn = new ArrayList<>();
if (isWithinPortal()) {
OrderByComparator comparator = OrderByComparatorFactoryUtil.create("User_", "screenname", true);
try {
_log.debug("Searching " + keyword);
List<User> lrUsers = UserLocalServiceUtil.search(ManagementUtils.getCompany().getCompanyId(), keyword, 0, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS, comparator);
for (User user : lrUsers) {
toReturn.add(new ItemBean(""+user.getUserId(), user.getScreenName(), user.getFullName(), getUserImagePortraitUrlLocal(user.getScreenName())));
}
} catch (SystemException | PortalException e) {
e.printStackTrace();
}
} else { //development
for (int i = 0; i < 10; i++) {
toReturn.add(new ItemBean("andrea.rossi", "andrea.rossi", "Andrea Rossi", "m.assante@gmail.com"));
if (i % 2 == 0)
toReturn.add(new ItemBean(""+i, "username"+i, "userGetFullname()"+i, "user.getEmail()"+i));
else
toReturn.add(new ItemBean(""+i, "ciccio"+i, "ciccioNome"+i, "ciccioEMail"+i));
}
}
return toReturn;
}
/**
* this method is needed because user images portrait change id depending on the portal instance
* e.g. a post made from iMarine portal would not show the avatarIMage in D4Science.org
* @param screenname
* @return the url of the image portrait for this portal instance
*/
private String getUserImagePortraitUrlLocal(String screenName) {
if (! isWithinPortal()) {
return "";
}
String thumbnailURL = "";
try {
thumbnailURL = new LiferayUserManager().getUserByUsername(screenName).getUserAvatarURL();
} catch (UserManagementSystemException | UserRetrievalFault e) {
e.printStackTrace();
}
return thumbnailURL;
}
/**
* utility method extract the @domain.com from an email address
* return @unknown-domain in case of no emails
*/
private String extractDomainFromEmail(String email) {
int index = email.indexOf('@');
if (index > 0)
return email.substring(index);
else
return "@unknown-domain";
}
}