private void addWorkers() { ResourceType type; ArrayList<Resource> users = null; ArrayList<Project> projects = null; Random random = new Random(1l); try { type = resourceTypeDAO.getResourceTypeByResourceTypeName("human"); users = resourceDAO.getResourcesByResourceType(type); projects = projectDAO.getAllProjects(); } catch (Exception e) { e.printStackTrace(); } for (Project project : projects) { int workers = random.nextInt(11) + 5; Resource user = users.get(random.nextInt(users.size())); try { resourceDAO.insertUserTask(user.getResourceID(), project.getProjectID(), true); } catch (UserWorkOnThisProjectException e) { e.printStackTrace(); } for (int i = 0; i < workers; ++i) { user = users.get(random.nextInt(users.size())); try { resourceDAO.insertUserTask( user.getResourceID(), project.getProjectID(), (random.nextInt(5) == 0)); } catch (UserWorkOnThisProjectException e) { System.out.println( "User already assigned, but don't worry, there are plenty to choose from"); } } } }
private void addDescriptions() throws IOException { RandomAccessFile f = new RandomAccessFile("randomDescription.csv", "r"); String dataString = null; Random random = new Random(1l); ArrayList<String> descriptions = new ArrayList<String>(); while ((dataString = f.readLine()) != null) { descriptions.add(dataString); } f.close(); try { ArrayList<Resource> resources = resourceDAO.getAllResources(); for (Resource r : resources) { r.setDescription(descriptions.get(random.nextInt(descriptions.size()))); resourceDAO.updateResource(r); } } catch (DAOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ResourceNameExistsException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ResourceHasActiveProjectException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Override @Transactional public void save(ResourceEntity entity, final String requestorId) { if (entity.getResourceType() != null) { entity.setResourceType(resourceTypeDao.findById(entity.getResourceType().getId())); } /* admin resource can't have an admin resource - do this check here */ boolean isAdminResource = StringUtils.equals(entity.getResourceType().getId(), adminResourceTypeId); if (entity.getType() != null && StringUtils.isNotBlank(entity.getType().getId())) { entity.setType(typeDAO.findById(entity.getType().getId())); } else { entity.setType(null); } if (StringUtils.isNotBlank(entity.getId())) { final ResourceEntity dbObject = resourceDao.findById(entity.getId()); entity.setAdminResource(dbObject.getAdminResource()); entity.setApproverAssociations(dbObject.getApproverAssociations()); if (isAdminResource) { entity.setAdminResource(null); } else if (entity.getAdminResource() == null) { final ResourceEntity adminResource = getNewAdminResource(entity, requestorId); entity.setAdminResource(adminResource); if (CollectionUtils.isEmpty(dbObject.getApproverAssociations())) { entity.addApproverAssociation(createDefaultApproverAssociations(entity, requestorId)); } } entity.setChildResources(dbObject.getChildResources()); entity.setParentResources(dbObject.getParentResources()); entity.setUsers(dbObject.getUsers()); entity.setGroups(dbObject.getGroups()); entity.setRoles(dbObject.getRoles()); // elementDAO.flush(); mergeAttribute(entity, dbObject); } else { boolean addApproverAssociation = false; if (isAdminResource) { entity.setAdminResource(null); } else { entity.setAdminResource(getNewAdminResource(entity, requestorId)); addApproverAssociation = true; } resourceDao.save(entity); if (addApproverAssociation) { entity.addApproverAssociation(createDefaultApproverAssociations(entity, requestorId)); } addRequiredAttributes(entity); } resourceDao.merge(entity); }
@Override @Transactional public void addChildResource(String parentResourceId, String childResourceId) { final ResourceEntity parent = resourceDao.findById(parentResourceId); final ResourceEntity child = resourceDao.findById(childResourceId); parent.addChildResource(child); resourceDao.save(parent); }
@Override @Transactional public void deleteResourceGroup(String resourceId, String groupId) { final ResourceEntity entity = resourceDao.findById(resourceId); final GroupEntity groupEntity = groupDao.findById(groupId); entity.remove(groupEntity); resourceDao.save(entity); }
@Override @Transactional public void deleteResourceRole(String resourceId, String roleId) { final ResourceEntity entity = resourceDao.findById(resourceId); final RoleEntity roleEntity = roleDao.findById(roleId); entity.remove(roleEntity); resourceDao.save(entity); }
@Override @Transactional public void deleteChildResource(String resourceId, String childResourceId) { final ResourceEntity parent = resourceDao.findById(resourceId); final ResourceEntity child = resourceDao.findById(childResourceId); parent.removeChildResource(child); resourceDao.save(parent); }
@Override @Transactional public void deleteResource(String resourceId) { if (StringUtils.isNotBlank(resourceId)) { final ResourceEntity entity = resourceDao.findById(resourceId); if (entity != null) { resourceDao.delete(entity); } } }
private void addResources() throws IOException { RandomAccessFile f = new RandomAccessFile("randomResource.csv", "r"); String dataString = null; ResourceType type = null; while ((dataString = f.readLine()) != null) { String[] data = dataString.split(";"); Resource insert = new Resource(); if (data[0].length() < 45) { insert.setResourceName(data[0]); } else { insert.setResourceName(data[0].substring(0, 44)); } try { insert.setResourceTypeID( resourceTypeDAO.getResourceTypeByResourceTypeName(data[1]).getResourceTypeID()); } catch (Exception e1) { e1.printStackTrace(); } insert.setDescription(""); insert.setActive(true); System.out.println(insert); try { resourceDAO.insertResource(insert); } catch (Exception e) { e.printStackTrace(); } } f.close(); }
@Override @Transactional(readOnly = true) public int count(ResourceSearchBean searchBean) { // final ResourceEntity entity = // resourceSearchBeanConverter.convert(searchBean); return resourceDao.count(searchBean); }
@Override @Transactional(readOnly = true) public List<Resource> getResourcesForGroupNoLocalized( String groupId, int from, int size, ResourceSearchBean searchBean) { List<ResourceEntity> resourceEntities = resourceDao.getResourcesForGroupNoLocalized(groupId, from, size, searchBean); return resourceConverter.convertToDTOList(resourceEntities, searchBean.isDeepCopy()); }
private void addNonHumanBookings() { ArrayList<Resource> resources = null; Random random = new Random(1l); try { resources = resourceDAO.getResourcesByResourceType( resourceTypeDAO.getResourceTypeByResourceTypeName("machine")); resources.addAll( resourceDAO.getResourcesByResourceType( resourceTypeDAO.getResourceTypeByResourceTypeName("room"))); } catch (Exception e) { e.printStackTrace(); } ArrayList<Project> allProjects = null; try { allProjects = projectDAO.getAllProjects(); } catch (Exception e) { e.printStackTrace(); } for (Resource resource : resources) { HashSet<Project> projects = new HashSet<Project>(); for (int i = 0; i < 3; ++i) { projects.add(allProjects.get(random.nextInt(allProjects.size()))); } int max = (projects.size() > 0) ? (50 / projects.size()) : 0; for (Project project : projects) { for (int i = project.getStartWeek(); i <= project.getDeadLine(); ++i) { if (random.nextInt(2) == 0) { Booking booking = new Booking(); booking.setProjectID(project.getProjectID()); booking.setResourceID(resource.getResourceID()); booking.setWeek(i); booking.setRatio(((float) (random.nextInt(max + 1) + max)) / 100); try { bookingDAO.insertBooking(booking); } catch (DAOException e) { e.printStackTrace(); } System.out.println(booking); } } } } }
@Override @Transactional(readOnly = true) public int getNumOfParentResources(String resourceId) { final ResourceEntity example = new ResourceEntity(); final ResourceEntity child = new ResourceEntity(); child.setId(resourceId); example.addChildResource(child); return resourceDao.count(example); }
@Override @Transactional(readOnly = true) public List<ResourceEntity> getParentResources(String resourceId, int from, int size) { final ResourceEntity example = new ResourceEntity(); final ResourceEntity child = new ResourceEntity(); child.setId(resourceId); example.addChildResource(child); return resourceDao.getByExample(example, from, size); }
@Override @Transactional(readOnly = true) @LocalizedServiceGet public List<ResourceEntity> findBeansLocalized( final ResourceSearchBean searchBean, final int from, final int size, final LanguageEntity language) { return resourceDao.getByExample(searchBean, from, size); }
@Override @Transactional(readOnly = true) public List<ResourceEntity> getChildResources(String resourceId, int from, int size) { final ResourceEntity example = new ResourceEntity(); final ResourceEntity parent = new ResourceEntity(); parent.setId(resourceId); example.addParentResource(parent); final List<ResourceEntity> resultList = resourceDao.getByExample(example, from, size); return resultList; }
@Override @Transactional public void validateResource2ResourceAddition(final String parentId, final String memberId) throws BasicDataServiceException { if (StringUtils.isBlank(parentId) || StringUtils.isBlank(memberId)) { throw new BasicDataServiceException( ResponseCode.INVALID_ARGUMENTS, "Parent ResourceId or Child ResourceId is null"); } final ResourceEntity parent = resourceDao.findById(parentId); final ResourceEntity child = resourceDao.findById(memberId); if (parent == null || child == null) { throw new BasicDataServiceException(ResponseCode.OBJECT_NOT_FOUND); } if (causesCircularDependency(parent, child, new HashSet<ResourceEntity>())) { throw new BasicDataServiceException(ResponseCode.CIRCULAR_DEPENDENCY); } if (parent.hasChildResoruce(child)) { throw new BasicDataServiceException(ResponseCode.RELATIONSHIP_EXISTS); } if (StringUtils.equals(parentId, memberId)) { throw new BasicDataServiceException(ResponseCode.CANT_ADD_YOURSELF_AS_CHILD); } if (parent.getResourceType() != null && !parent.getResourceType().isSupportsHierarchy()) { throw new BasicDataServiceException( ResponseCode.RESOURCE_TYPE_NOT_SUPPORTS_HIERARCHY, parent.getResourceType().getDescription()); } if (child.getResourceType() != null && !child.getResourceType().isSupportsHierarchy()) { throw new BasicDataServiceException( ResponseCode.RESOURCE_TYPE_NOT_SUPPORTS_HIERARCHY, child.getResourceType().getDescription()); } }
private void addHumanBookings() { ArrayList<Resource> workers = null; Random random = new Random(1l); try { workers = resourceDAO.getResourcesByResourceType( resourceTypeDAO.getResourceTypeByResourceTypeName("human")); } catch (Exception e) { e.printStackTrace(); } for (Resource worker : workers) { ArrayList<Project> projects = null; try { projects = projectDAO.getProjectsByWorker(worker); } catch (Exception e) { e.printStackTrace(); } int max = (projects.size() > 0) ? (50 / projects.size()) : 0; for (Project project : projects) { for (int i = project.getStartWeek(); i <= project.getDeadLine(); ++i) { if (random.nextInt(2) == 0) { Booking booking = new Booking(); booking.setProjectID(project.getProjectID()); booking.setResourceID(worker.getResourceID()); booking.setWeek(i); booking.setRatio(((float) (random.nextInt(max + 1) + max)) / 100); try { bookingDAO.insertBooking(booking); } catch (DAOException e) { e.printStackTrace(); } System.out.println(booking); } } } } }
@Override @Transactional public void validateResourceDeletion(final String resourceId) throws BasicDataServiceException { final ResourceEntity entity = resourceDao.findById(resourceId); if (entity != null) { final List<ManagedSysEntity> managedSystems = managedSysDAO.findByResource(resourceId); if (CollectionUtils.isNotEmpty(managedSystems)) { throw new BasicDataServiceException( ResponseCode.LINKED_TO_MANAGED_SYSTEM, managedSystems.get(0).getName()); } final List<ContentProviderEntity> contentProviders = contentProviderDAO.getByResourceId(resourceId); if (CollectionUtils.isNotEmpty(contentProviders)) { throw new BasicDataServiceException( ResponseCode.LINKED_TO_CONTENT_PROVIDER, contentProviders.get(0).getName()); } final List<URIPatternEntity> uriPatterns = uriPatternDAO.getByResourceId(resourceId); if (CollectionUtils.isNotEmpty(uriPatterns)) { throw new BasicDataServiceException( ResponseCode.LINKED_TO_URI_PATTERN, uriPatterns.get(0).getPattern()); } final List<AuthProviderEntity> authProviders = authProviderDAO.getByResourceId(resourceId); if (CollectionUtils.isNotEmpty(authProviders)) { throw new BasicDataServiceException( ResponseCode.LINKED_TO_AUTHENTICATION_PROVIDER, authProviders.get(0).getName()); } final List<MetadataElementEntity> metadataElements = elementDAO.getByResourceId(resourceId); if (CollectionUtils.isNotEmpty(metadataElements)) { throw new BasicDataServiceException( ResponseCode.LINKED_TO_METADATA_ELEMENT, metadataElements.get(0).getAttributeName()); } final List<MetadataElementPageTemplateEntity> pageTemplates = templateDAO.getByResourceId(resourceId); if (CollectionUtils.isNotEmpty(pageTemplates)) { throw new BasicDataServiceException( ResponseCode.LINKED_TO_PAGE_TEMPLATE, pageTemplates.get(0).getName()); } final ResourceEntity searchBean = new ResourceEntity(); searchBean.setAdminResource(new ResourceEntity(resourceId)); final List<ResourceEntity> adminOfResources = resourceDao.getByExample(searchBean); if (CollectionUtils.isNotEmpty(adminOfResources)) { throw new BasicDataServiceException( ResponseCode.RESOURCE_IS_AN_ADMIN_OF_RESOURCE, adminOfResources.get(0).getName()); } final RoleEntity roleSearchBean = new RoleEntity(); roleSearchBean.setAdminResource(new ResourceEntity(resourceId)); final List<RoleEntity> adminOfRoles = roleDao.getByExample(roleSearchBean); if (CollectionUtils.isNotEmpty(adminOfRoles)) { throw new BasicDataServiceException( ResponseCode.RESOURCE_IS_AN_ADMIN_OF_ROLE, adminOfRoles.get(0).getName()); } final GroupEntity groupSearchBean = new GroupEntity(); groupSearchBean.setAdminResource(new ResourceEntity(resourceId)); final List<GroupEntity> adminOfGroups = groupDao.getByExample(groupSearchBean); if (CollectionUtils.isNotEmpty(adminOfGroups)) { throw new BasicDataServiceException( ResponseCode.RESOURCE_IS_AN_ADMIN_OF_GROUP, adminOfGroups.get(0).getName()); } final OrganizationEntity orgSearchBean = new OrganizationEntity(); orgSearchBean.setAdminResource(new ResourceEntity(resourceId)); final List<OrganizationEntity> adminOfOrgs = orgDAO.getByExample(orgSearchBean); if (CollectionUtils.isNotEmpty(adminOfOrgs)) { throw new BasicDataServiceException( ResponseCode.RESOURCE_IS_AN_ADMIN_OF_ORG, adminOfOrgs.get(0).getName()); } } }
@Override @Transactional(readOnly = true) public List<ResourceEntity> findBeans( final ResourceSearchBean searchBean, final int from, final int size) { return resourceDao.getByExampleNoLocalize(searchBean, from, size); }
private void addUsers() throws IOException { RandomAccessFile f = new RandomAccessFile("randomUser.csv", "r"); String dataString = null; ResourceType type = null; try { type = resourceTypeDAO.getResourceTypeByResourceTypeName("human"); } catch (Exception e) { System.err.println(e); } ArrayList<Group> groups = null; Random random = new Random(1l); try { groups = groupDAO.getAllGroups(); } catch (DAOException e1) { e1.printStackTrace(); } while ((dataString = f.readLine()) != null) { String[] data = dataString.split(";"); User insert = new User(); if (data[0].length() < 45) { insert.setUserName(data[0]); } else { insert.setUserName(data[0].substring(0, 44)); } insert.setPassword(Hash.hashString(data[1])); if (data[2].length() < 15) { insert.setPhoneNumber(data[2]); } else { insert.setPhoneNumber(data[2].substring(0, 14)); } if (data[3].length() < 45) { insert.setEmail(data[3]); } else { insert.setEmail(data[3].substring(0, 44)); } if (data[4].length() < 45) { insert.setAddress(data[4]); } else { insert.setAddress(data[4].substring(0, 44)); } Resource pair = new Resource(); pair.setActive(true); pair.setDescription(""); pair.setResourceTypeID(type.getResourceTypeID()); pair.setResourceName(data[5]); System.out.println(insert); try { userDAO.insertUser(insert); resourceDAO.insertResource(pair); resourceDAO.linkResourceToUser(pair, insert); } catch (Exception e) { e.printStackTrace(); } for (int i = 0; i < 3; ++i) { try { resourceDAO.addResourceToGroup(pair, groups.get(random.nextInt(groups.size()))); } catch (DAOException e) { System.out.println( "Resource already added to group, but don't worry, there are plenty to choose from"); } } } f.close(); }
@Override @Transactional(readOnly = true) public ResourceEntity findResourceByName(String name) { return resourceDao.findByName(name); }
@Override @Transactional(readOnly = true) public Resource getResourceDTO(String resourceId) { return resourceConverter.convertToDTO(resourceDao.findById(resourceId), true); }
@Override @Transactional(readOnly = true) public List<ResourceEntity> findResourcesByIds(Collection<String> resourceIdCollection) { return resourceDao.findByIds(resourceIdCollection); }
@Override @Transactional(readOnly = true) public List<ResourceEntity> getResourcesForUserByType( String userId, String resourceTypeId, final ResourceSearchBean searchBean) { return resourceDao.getResourcesForUserByType(userId, resourceTypeId, searchBean); }
@Override @Transactional(readOnly = true) public List<ResourceEntity> getResourcesForUser( String userId, int from, int size, final ResourceSearchBean searchBean) { return resourceDao.getResourcesForUser(userId, from, size, searchBean); }
@Override @Transactional(readOnly = true) public int getNumOfResourceForUser(String userId, final ResourceSearchBean searchBean) { return resourceDao.getNumOfResourcesForUser(userId, searchBean); }
@Override @Transactional(readOnly = true) public ResourceEntity findResourceByIdNoLocalized(String resourceId) { return resourceDao.findByIdNoLocalized(resourceId); }
@Override @Transactional(readOnly = true) public int getNumOfResourcesForRole(String roleId, final ResourceSearchBean searchBean) { return resourceDao.getNumOfResourcesForRole(roleId, searchBean); }