class DocumentServiceImpl extends RemoteServiceServlet implements DocumentService {

	@Override
	public void linkDocuments(long[] inDocIds, long[] outDocIds) throws ServerException {
		Session session = ServiceUtil.validateSession(getThreadLocalRequest());

		DocumentLinkDAO linkDao = (DocumentLinkDAO) Context.get().getBean(DocumentLinkDAO.class);
		DocumentDAO docDao = (DocumentDAO) Context.get().getBean(DocumentDAO.class);
		if (outDocIds.length > 0) {
			try {
				for (int i = 0; i < inDocIds.length; i++) {
					for (int j = 0; j < outDocIds.length; j++) {
						DocumentLink link = linkDao.findByDocIdsAndType(inDocIds[i], outDocIds[j], "default");
						if (link == null) {
							// The link doesn't exist and must be created
							link = new DocumentLink();
							link.setTenantId(session.getTenantId());
							link.setDocument1(docDao.findById(inDocIds[i]));
							link.setDocument2(docDao.findById(outDocIds[j]));
							link.setType("default");
							linkDao.store(link);
						}
					}
				}
			} catch (Throwable t) {
				ServiceUtil.throwServerException(session, log, t);
			}
		}
	}

}