class DocumentServiceImpl extends RemoteServiceServlet implements DocumentService {

	@Override
	public GUIVersion[] getVersionsById(long id1, long id2) throws ServerException {
		Session session = ServiceUtil.validateSession(getThreadLocalRequest());

		try {
			VersionDAO versDao = (VersionDAO) Context.get().getBean(VersionDAO.class);
			Version docVersion = versDao.findById(id1);
			versDao.initialize(docVersion);

			GUIVersion version1 = null;
			if (docVersion != null) {
				version1 = new GUIVersion();
				version1.setDocId(docVersion.getDocId());
				version1.setUsername(docVersion.getUsername());
				version1.setComment(docVersion.getComment());
				version1.setId(id1);
				version1.setCustomId(docVersion.getCustomId());
				version1.setTagsString(docVersion.getTgs());
				version1.setType(docVersion.getType());
				version1.setFileName(docVersion.getFileName());
				version1.setCreation(docVersion.getCreation());
				version1.setCreator(docVersion.getCreator());
				version1.setDate(docVersion.getDate());
				version1.setPublisher(docVersion.getPublisher());
				version1.setVersion(docVersion.getVersion());
				version1.setFileVersion(docVersion.getFileVersion());
				version1.setLanguage(docVersion.getLanguage());
				version1.setTemplateId(docVersion.getTemplateId());
				version1.setFileSize(Float.valueOf(docVersion.getFileSize()));
				version1.setWorkflowStatus(docVersion.getWorkflowStatus());
				version1.setWorkflowStatusDisplay(docVersion.getWorkflowStatusDisplay());
				if (docVersion.getRating() != null)
					version1.setRating(docVersion.getRating());
				version1.setStartPublishing(docVersion.getStartPublishing());
				version1.setStopPublishing(docVersion.getStopPublishing());
				version1.setPublished(docVersion.getPublished());
				version1.setPages(docVersion.getPages());
				version1.setOcrd(docVersion.getOcrd());
				version1.setOcrTemplateId(docVersion.getOcrTemplateId());

				version1.setTemplate(docVersion.getTemplateName());
				versDao.initialize(docVersion);
				for (String attrName : docVersion.getAttributeNames()) {
					Attribute extAttr = docVersion.getAttributes().get(attrName);
					GUIAttribute att = new GUIAttribute();
					att.setName(attrName);
					att.setSetId(extAttr.getSetId());
					att.setPosition(extAttr.getPosition());
					att.setLabel(extAttr.getLabel());
					att.setMandatory(extAttr.getMandatory() == 1);
					att.setHidden(extAttr.getHidden() == 1);
					att.setMultiple(extAttr.getMultiple() == 1);
					att.setParent(extAttr.getParent());
					att.setStringValues(extAttr.getStringValues());
					att.setEditor(extAttr.getEditor());
					att.setStringValue(extAttr.getStringValue());
					att.setIntValue(extAttr.getIntValue());
					att.setBooleanValue(extAttr.getBooleanValue());
					att.setDoubleValue(extAttr.getDoubleValue());
					att.setType(extAttr.getType());
					version1.addAttribute(att);
				}
				GUIFolder folder1 = new GUIFolder();
				folder1.setName(docVersion.getFolderName());
				folder1.setId(docVersion.getFolderId());
				version1.setFolder(folder1);
			}

			docVersion = versDao.findById(id2);
			versDao.initialize(docVersion);

			GUIVersion version2 = null;
			if (docVersion != null) {
				version2 = new GUIVersion();
				version2.setDocId(docVersion.getDocId());
				version2.setUsername(docVersion.getUsername());
				version2.setComment(docVersion.getComment());
				version2.setId(id1);
				version2.setCustomId(docVersion.getCustomId());
				version2.setTagsString(docVersion.getTgs());
				version2.setType(docVersion.getType());
				version2.setFileName(docVersion.getFileName());
				version2.setCreation(docVersion.getCreation());
				version2.setCreator(docVersion.getCreator());
				version2.setDate(docVersion.getDate());
				version2.setPublisher(docVersion.getPublisher());
				version2.setVersion(docVersion.getVersion());
				version2.setFileVersion(docVersion.getFileVersion());
				version2.setLanguage(docVersion.getLanguage());
				version2.setFileSize(Float.valueOf(docVersion.getFileSize()));
				if (docVersion.getRating() != null)
					version2.setRating(docVersion.getRating());
				version2.setWorkflowStatus(docVersion.getWorkflowStatus());
				version2.setStartPublishing(docVersion.getStartPublishing());
				version2.setStopPublishing(docVersion.getStopPublishing());
				version2.setPublished(docVersion.getPublished());
				version2.setPages(docVersion.getPages());
				version2.setOcrd(docVersion.getOcrd());
				version2.setOcrTemplateId(docVersion.getOcrTemplateId());
				version2.setBarcodeTemplateId(docVersion.getBarcodeTemplateId());

				version2.setTemplateId(docVersion.getTemplateId());
				version2.setTemplate(docVersion.getTemplateName());
				versDao.initialize(docVersion);
				for (String attrName : docVersion.getAttributeNames()) {
					Attribute extAttr = docVersion.getAttributes().get(attrName);
					GUIAttribute att = new GUIAttribute();
					att.setName(attrName);
					att.setSetId(extAttr.getSetId());
					att.setPosition(extAttr.getPosition());
					att.setLabel(extAttr.getLabel());
					att.setMandatory(extAttr.getMandatory() == 1);
					att.setHidden(extAttr.getHidden() == 1);
					att.setMultiple(extAttr.getMultiple() == 1);
					att.setParent(extAttr.getParent());
					att.setEditor(extAttr.getEditor());
					att.setStringValue(extAttr.getStringValue());
					att.setIntValue(extAttr.getIntValue());
					att.setBooleanValue(extAttr.getBooleanValue());
					att.setDoubleValue(extAttr.getDoubleValue());
					att.setType(extAttr.getType());
					version2.addAttribute(att);
				}
				GUIFolder folder2 = new GUIFolder();
				folder2.setName(docVersion.getFolderName());
				folder2.setId(docVersion.getFolderId());
				version2.setFolder(folder2);
			}

			GUIVersion[] versions = null;
			if (version1 != null && version2 != null) {
				versions = new GUIVersion[2];
				versions[0] = version1;
				versions[1] = version2;
			} else if (version1 != null && version2 == null) {
				versions = new GUIVersion[1];
				versions[0] = version1;
			} else if (version1 == null && version2 != null) {
				versions = new GUIVersion[1];
				versions[0] = version2;
			} else
				return null;

			return versions;
		} catch (Throwable t) {
			log.error("Exception linking documents: {}", t.getMessage(), t);
			return (GUIVersion[]) ServiceUtil.throwServerException(session, null, t);
		}
	}

}