Java Code Examples for javax.jcr.Node#getProperty()

The following examples show how to use javax.jcr.Node#getProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseRepositoryService.java    From urule with Apache License 2.0 8 votes vote down vote up
@Override
public InputStream readFile(String path,String version) throws Exception{
	if(StringUtils.isNotBlank(version)){
		repositoryInteceptor.readFile(path+":"+version);
		return readVersionFile(path, version);
	}
	repositoryInteceptor.readFile(path);
	Node rootNode=getRootNode();
	int colonPos = path.lastIndexOf(":");
	if (colonPos > -1) {
		version = path.substring(colonPos + 1, path.length());
		path = path.substring(0, colonPos);
		return readFile(path, version);
	}
	path = processPath(path);
	if (!rootNode.hasNode(path)) {
		throw new RuleException("File [" + path + "] not exist.");
	}
	Node fileNode = rootNode.getNode(path);
	Property property = fileNode.getProperty(DATA);
	Binary fileBinary = property.getBinary();
	return fileBinary.getStream();
}
 
Example 2
Source File: Sync.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
public boolean load(VltContext ctx) throws RepositoryException {
    if (!s.nodeExists(CFG_NODE_PATH)) {
        return false;
    }
    Node cfgNode = s.getNode(CFG_NODE_PATH);
    if (cfgNode.hasProperty(CFG_ENABLED)) {
        enabled = cfgNode.getProperty(CFG_ENABLED).getBoolean();
    }
    if (cfgNode.hasProperty(CFG_ROOTS)) {
        Property roots = cfgNode.getProperty(CFG_ROOTS);
        for (Value v : roots.getValues()) {
            this.roots.add(v.getString());
        }
    }
    return true;
}
 
Example 3
Source File: StorageUpdate8.java    From nextreports-server with Apache License 2.0 6 votes vote down vote up
private void updateInternalSettings() throws RepositoryException {
	// find all internalSettings nodes from DASHBOARDS and change chartId property in entityId
	String statement = "/jcr:root" + ISO9075.encodePath(StorageConstants.DASHBOARDS_ROOT) + "//*[fn:name()='internalSettings']";
    QueryResult queryResult = getTemplate().query(statement);
    NodeIterator nodes = queryResult.getNodes();
    LOG.info("Found " + nodes.getSize() +  " internalSettings nodes");
    while (nodes.hasNext()) {
    	Node node = nodes.nextNode();
    	try {
    		Property prop = node.getProperty("chartId");
    		node.setProperty("entityId", prop.getValue());
    		prop.remove();
    	} catch (PathNotFoundException ex) {
    		// if property not found we have nothing to do
    	}
    } 	
}
 
Example 4
Source File: BaseRepositoryService.java    From urule with Apache License 2.0 5 votes vote down vote up
@Override
public List<VersionFile> getVersionFiles(String path) throws Exception{
	path = processPath(path);
	Node rootNode=getRootNode();
	if (!rootNode.hasNode(path)) {
		throw new RuleException("File [" + path + "] not exist.");
	}
	List<VersionFile> files = new ArrayList<VersionFile>();
	Node fileNode = rootNode.getNode(path);
	VersionHistory versionHistory = versionManager.getVersionHistory(fileNode.getPath());
	VersionIterator iterator = versionHistory.getAllVersions();
	while (iterator.hasNext()) {
		Version version = iterator.nextVersion();
		String versionName = version.getName();
		if (versionName.startsWith("jcr:")) {
			continue; // skip root version
		}
		Node fnode = version.getFrozenNode();
		VersionFile file = new VersionFile();
		file.setName(version.getName());
		file.setPath(fileNode.getPath());
		Property prop = fnode.getProperty(CRATE_USER);
		file.setCreateUser(prop.getString());
		prop = fnode.getProperty(CRATE_DATE);
		file.setCreateDate(prop.getDate().getTime());
		if(fnode.hasProperty(VERSION_COMMENT)){
			prop=fnode.getProperty(VERSION_COMMENT);
			file.setComment(prop.getString());
		}
		files.add(file);
	}
	return files;
}
 
Example 5
Source File: BaseRepositoryService.java    From urule with Apache License 2.0 5 votes vote down vote up
private InputStream readVersionFile(String path, String version) throws Exception{
	path = processPath(path);
	Node rootNode=getRootNode();
	if (!rootNode.hasNode(path)) {
		throw new RuleException("File [" + path + "] not exist.");
	}
	Node fileNode = rootNode.getNode(path);
	VersionHistory versionHistory = versionManager.getVersionHistory(fileNode.getPath());
	Version v = versionHistory.getVersion(version);
	Node fnode = v.getFrozenNode();
	Property property = fnode.getProperty(DATA);
	Binary fileBinary = property.getBinary();
	return fileBinary.getStream();
}
 
Example 6
Source File: RepositoryServiceImpl.java    From urule with Apache License 2.0 5 votes vote down vote up
@Override
public List<UserPermission> loadResourceSecurityConfigs(String companyId) throws Exception{
	List<UserPermission> configs=new ArrayList<UserPermission>();
	String filePath=RESOURCE_SECURITY_CONFIG_FILE+(companyId == null ? "" : companyId);
	Node rootNode=getRootNode();
	Node fileNode = rootNode.getNode(filePath);
	Property property = fileNode.getProperty(DATA);
	Binary fileBinary = property.getBinary();
	InputStream inputStream = fileBinary.getStream();
	String content = IOUtils.toString(inputStream, "utf-8");
	inputStream.close();
	Document document = DocumentHelper.parseText(content);
	Element rootElement = document.getRootElement();
	for (Object obj : rootElement.elements()) {
		if (!(obj instanceof Element)) {
			continue;
		}
		Element element = (Element) obj;
		if (!element.getName().equals("user-permission")) {
			continue;
		}
		UserPermission userResource=new UserPermission();
		userResource.setUsername(element.attributeValue("username"));
		userResource.setProjectConfigs(parseProjectConfigs(element));
		configs.add(userResource);
	}
	return configs;
}
 
Example 7
Source File: RepositoryServiceImpl.java    From urule with Apache License 2.0 5 votes vote down vote up
@Override
public List<ClientConfig> loadClientConfigs(String project) throws Exception{
	if(!permissionService.isAdmin()){
		throw new NoPermissionException();
	}
	List<ClientConfig> clients=new ArrayList<ClientConfig>();
	Node rootNode=getRootNode();
	String filePath = processPath(project) + "/" + CLIENT_CONFIG_FILE;
	Node fileNode = rootNode.getNode(filePath);
	Property property = fileNode.getProperty(DATA);
	Binary fileBinary = property.getBinary();
	InputStream inputStream = fileBinary.getStream();
	String content = IOUtils.toString(inputStream, "utf-8");
	inputStream.close();
	Document document = DocumentHelper.parseText(content);
	Element rootElement = document.getRootElement();
	for (Object obj : rootElement.elements()) {
		if (!(obj instanceof Element)) {
			continue;
		}
		Element element = (Element) obj;
		if (!element.getName().equals("item")) {
			continue;
		}
		ClientConfig client = new ClientConfig();
		client.setName(element.attributeValue("name"));
		client.setClient(element.attributeValue("client"));
		client.setProject(project);
		clients.add(client);
	}
	return clients;
}
 
Example 8
Source File: JCRBuilder.java    From jackalope with Apache License 2.0 5 votes vote down vote up
public Property build(Node parent) {
    if (parent == null) return null; // properties only exist in nodes so there is nothing to build.
    try {
        if (hasMultiple)
            parent.setProperty(name, values);
        else
            parent.setProperty(name, values[0]);
        return parent.getProperty(name);
    }
    catch (RepositoryException re) {
        throw new SlingRepositoryException(re);
    }
}
 
Example 9
Source File: StorageUpdate11.java    From nextreports-server with Apache License 2.0 5 votes vote down vote up
private void convertReports() throws RepositoryException {
	
	String statement = 
			"/jcr:root" + ISO9075.encodePath(StorageConstants.REPORTS_ROOT) + 
			"//*[@className='ro.nextreports.server.domain.Report' and @type='Next']" + 
			"//*[fn:name()='jcr:content' and @jcr:mimeType='text/xml']";
	  
	QueryResult queryResult = getTemplate().query(statement);

	NodeIterator nodes = queryResult.getNodes();
	LOG.info("Converter 5.1 : Found " + nodes.getSize() + " report nodes");
	while (nodes.hasNext()) {
		
		Node node = nodes.nextNode();
		
		Node reportNode = node.getParent().getParent().getParent().getParent();
		String reportName = reportNode.getName();
		String reportPath = reportNode.getPath();	
		LOG.info(" * Start convert '" + reportPath + "'");						
											
		Property prop = node.getProperty("jcr:data");			
       	String xml = null;
           try {                  	
           	xml = new Converter_5_2().convertFromInputStream(prop.getBinary().getStream(), true);            	            	
           	if (xml != null) {
               	ValueFactory valueFactory = node.getSession().getValueFactory(); 
               	Binary binaryValue = valueFactory.createBinary(new ByteArrayInputStream(xml.getBytes("UTF-8")));
               	node.setProperty ("jcr:data", binaryValue);                	
               	LOG.info("\t -> OK");
               } else {
               	LOG.error("\t -> FAILED : null xml");
               }
           	            	            	
           } catch (Throwable t) {                    	            	            
           	LOG.error("\t-> FAILED : " + t.getMessage(), t);            	
           } 					
           
	}
}
 
Example 10
Source File: StorageUpdater.java    From nextreports-server with Apache License 2.0 5 votes vote down vote up
private void resetFirstUsageDates() throws Exception {
  	LOG.info("Reset firstUsage.date for all users");
String statement = "/jcr:root"
		+ ISO9075.encodePath(StorageConstants.USERS_ROOT)
		+ "//*[@className='ro.nextreports.server.domain.UserPreferences']";
QueryResult queryResult = jcrTemplate.query(statement);

NodeIterator nodes = queryResult.getNodes();
LOG.info("Found " + nodes.getSize() + " user preferences nodes");		
while (nodes.hasNext()) {
	Node node = nodes.nextNode();
	
	Node pNode = node.getNode("preferences");
	try {
		Property property = pNode.getProperty("firstUsage.date");
		if (property.getValue() != null) {
			LOG.info("    removed firstUsage.date = " + property.getString() + "  for user " + node.getParent().getName());
			String s = null;
			pNode.setProperty("firstUsage.date", s);
		}
	} catch (PathNotFoundException ex) {
		// nothing to do
	}
}

jcrTemplate.save();
  }
 
Example 11
Source File: BaseRepositoryService.java    From urule with Apache License 2.0 4 votes vote down vote up
@Override
public List<ResourcePackage> loadProjectResourcePackages(String project) throws Exception {
	Node rootNode=getRootNode();
	String filePath = processPath(project) + "/" + RES_PACKGE_FILE;
	SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	Node fileNode = rootNode.getNode(filePath);
	Property property = fileNode.getProperty(DATA);
	Binary fileBinary = property.getBinary();
	InputStream inputStream = fileBinary.getStream();
	String content = IOUtils.toString(inputStream, "utf-8");
	inputStream.close();
	Document document = DocumentHelper.parseText(content);
	Element rootElement = document.getRootElement();
	List<ResourcePackage> packages = new ArrayList<ResourcePackage>();
	for (Object obj : rootElement.elements()) {
		if (!(obj instanceof Element)) {
			continue;
		}
		Element element = (Element) obj;
		if (!element.getName().equals("res-package")) {
			continue;
		}
		ResourcePackage p = new ResourcePackage();
		String dateStr = element.attributeValue("create_date");
		if (dateStr != null) {
			p.setCreateDate(sd.parse(dateStr));
		}
		p.setId(element.attributeValue("id"));
		p.setName(element.attributeValue("name"));
		p.setProject(project);
		List<ResourceItem> items = new ArrayList<ResourceItem>();
		for (Object o : element.elements()) {
			if (!(o instanceof Element)) {
				continue;
			}
			Element ele = (Element) o;
			if (!ele.getName().equals("res-package-item")) {
				continue;
			}
			ResourceItem item = new ResourceItem();
			item.setName(ele.attributeValue("name"));
			item.setPackageId(p.getId());
			item.setPath(ele.attributeValue("path"));
			item.setVersion(ele.attributeValue("version"));
			items.add(item);
		}
		p.setResourceItems(items);
		packages.add(p);
	}
	return packages;
}
 
Example 12
Source File: DocViewProperty.java    From jackrabbit-filevault with Apache License 2.0 4 votes vote down vote up
/**
 * Sets this property on the given node
 *
 * @param node the node
 * @return {@code true} if the value was modified.
 * @throws RepositoryException if a repository error occurs
 */
public boolean apply(Node node) throws RepositoryException {
    Property prop = node.hasProperty(name) ? node.getProperty(name) : null;
    // check if multiple flags are equal
    if (prop != null && isMulti != prop.getDefinition().isMultiple()) {
        prop.remove();
        prop = null;
    }
    if (prop != null) {
        int propType = prop.getType();
        if (propType != type && (propType != PropertyType.STRING || type != PropertyType.UNDEFINED)) {
            // never compare if types differ
            prop = null;
        }
    }
    if (isMulti) {
        // todo: handle multivalue binaries and reference binaries
        Value[] vs = prop == null ? null : prop.getValues();
        if (vs != null && vs.length == values.length) {
            // quick check all values
            boolean modified = false;
            for (int i=0; i<vs.length; i++) {
                if (!vs[i].getString().equals(values[i])) {
                    modified = true;
                }
            }
            if (!modified) {
                return false;
            }
        }
        if (type == PropertyType.UNDEFINED) {
            node.setProperty(name, values);
        } else {
            node.setProperty(name, values, type);
        }
        // assume modified
        return true;
    } else {
        Value v = prop == null ? null : prop.getValue();
        if (type == PropertyType.BINARY) {
            if (isReferenceProperty) {
                ReferenceBinary ref = new SimpleReferenceBinary(values[0]);
                Binary binary = node.getSession().getValueFactory().createValue(ref).getBinary();
                if (v != null) {
                    Binary bin = v.getBinary();
                    if (bin.equals(binary)) {
                        return false;
                    }
                }
                node.setProperty(name, binary);
            }
            // the binary property is always modified (TODO: check if still correct with JCRVLT-110)
            return true;
        }
        if (v == null || !v.getString().equals(values[0])) {
            try {
                if (type == PropertyType.UNDEFINED) {
                    node.setProperty(name, values[0]);
                } else {
                    node.setProperty(name, values[0], type);
                }
            } catch (ValueFormatException e) {
                // forcing string
                node.setProperty(name, values[0], PropertyType.STRING);
            }
            return true;
        }
    }
    return false;
}
 
Example 13
Source File: JcrPackageImpl.java    From jackrabbit-filevault with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Nullable
public Property getData() throws RepositoryException {
    Node content = getContent();
    return content == null ? null : content.getProperty(JcrConstants.JCR_DATA);
}