简体   繁体   中英

ContentModel cannot be resolved to a variable

I'm having this error :

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    ContentModel cannot be resolved to a variable

    at test2CMIS.Test.main(Test.java:39)" 

And I dont understand from where it comes, here is my code :

public class Test {

    public static void main(String[] args){
        Test atest = new Test();
        Session session = atest.iniSession();
        AuthenticationService authenticationService=null;
        PersonService personService = null;

        if (authenticationService.authenticationExists("test") == false)
        {
           authenticationService.createAuthentication("test", "changeMe".toCharArray());

           PropertyMap ppOne = new PropertyMap(4);
           ppOne.put(ContentModel.PROP_USERNAME, "test");
           ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName");
           ppOne.put(ContentModel.PROP_LASTNAME, "lastName");
           ppOne.put(ContentModel.PROP_EMAIL, "test"+"@example.com");

           personService.createPerson(ppOne);
        }
    }

I did import the : import org.alfresco.model.ContentModel; and a lot of others librarys for my code.

Thx for help.

The code I'm using and I left some things that I tried too in comments so you can see what things I have done:

import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import org.alfresco.service.cmr.security.*;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;


import java.util.Iterator;

import org.alfresco.repo.jscript.People;
import org.alfresco.repo.security.authentication.AuthenticationException;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.PropertyMap;
import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.commons.PropertyIds;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.apache.chemistry.opencmis.commons.enums.VersioningState;
import org.apache.chemistry.opencmis.commons.exceptions.CmisContentAlreadyExistsException;
import org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException;
import org.apache.chemistry.opencmis.client.util.FileUtils;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;

public class Test {

    public static void main(String[] args){
        Test atest = new Test();
        Session session = atest.iniSession();
        AuthenticationService authenticationService=new AuthenticationServiceImpl();
        PersonService personService = new PersonServiceImpl();


        HashMap<QName, Serializable> properties = new HashMap<QName, Serializable>();
        properties.put(ContentModel.PROP_USERNAME, "test");
        properties.put(ContentModel.PROP_FIRSTNAME, "test");
        properties.put(ContentModel.PROP_LASTNAME, "qsdqsd");
        properties.put(ContentModel.PROP_EMAIL, "wshAlors@gmail.com");
        properties.put(ContentModel.PROP_ENABLED, Boolean.valueOf(true));
        properties.put(ContentModel.PROP_ACCOUNT_LOCKED, Boolean.valueOf(false));
        personService.createPerson(properties);

        authenticationService.createAuthentication("test", "changeme".toCharArray());

        authenticationService.setAuthenticationEnabled("test", true);

        authenticationService.getAuthenticationEnabled("Admin");

        //String testAuthen = authenticationService.getCurrentTicket();
        //System.out.println(testAuthen);
        //QName username = QName.createQName("test");
        //Map<QName,Serializable> propertiesUser = new HashMap<QName,Serializable>();
        //propertiesUser.put(ContentModel.PROP_USERNAME,username);
        //propertiesUser.put(ContentModel.PROP_FIRSTNAME,"test");
        //propertiesUser.put(ContentModel.PROP_LASTNAME,"test");
        //propertiesUser.put(ContentModel.PROP_EMAIL, "test@example.com");
        //propertiesUser.put(ContentModel.PROP_PASSWORD,"0000");
        //personService.createPerson(propertiesUser);



        //if (authenticationService.authenticationExists("test") == false)
        //{
        //   authenticationService.createAuthentication("test", "changeMe".toCharArray());

        //   PropertyMap ppOne = new PropertyMap(4);
        //   ppOne.put(ContentModel.PROP_USERNAME, "test");
        //   ppOne.put(ContentModel.PROP_FIRSTNAME, "test");
        //   ppOne.put(ContentModel.PROP_LASTNAME, "test");
        //   ppOne.put(ContentModel.PROP_EMAIL, "test@example.com");
           //ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle");

        //   personService.createPerson(ppOne);
        //}
    }

    public  Session iniSession() {
        Session session;
        SessionFactoryImpl sf = SessionFactoryImpl.newInstance();
        Map<String, String> parameters = new HashMap<String, String>();
        Scanner reader = new Scanner(System.in);
        System.out.println("Enter your logging : ");
        String log = reader.nextLine();
        System.out.println("Enter your password : ");
        String pass = reader.nextLine();

        parameters.put(SessionParameter.USER, log);
        parameters.put(SessionParameter.PASSWORD, pass);

        parameters.put(SessionParameter.BROWSER_URL, "http://127.0.0.1:8080/alfresco/api/-default-/public/cmis/versions/1.1/browser");
        parameters.put(SessionParameter.BINDING_TYPE, BindingType.BROWSER.value());
        parameters.put(SessionParameter.REPOSITORY_ID, "-default-");

        try{
            session = sf.createSession(parameters);
        }catch(CmisUnauthorizedException cue){
            session = null;
            System.out.println("Wrong logging OR password !");
        }
        return session;
    }

You are writing a runnable class which is not running the same process as Alfresco. In that sense, your class is running "remotely".

Because your class is running remotely to Alfresco, you are correct in using CMIS. But CMIS will only allow you to perform Create, Read, Update, and Delete (CRUD) functions against documents and folders in Alfresco. CMIS does not know how to create users or groups.

Your class will not be able to instantiate the AuthenticationService or PersonService. Those are part of the Alfresco Foundation API which only works when you are running in the same process as Alfresco, such as in an Action, a Behavior, or a Java-backed web script. In those cases, you will use Spring Dependency Injection to inject those services into your Java class. You would then put your class in a JAR that gets deployed into the Alfresco web application and loaded by the same classloader as Alfresco's.

If you want to create users remotely you should consider using the Alfresco REST API . Your runnable class can then use an HTTP client to invoke REST calls to create people and groups.

Thanks you for everything ! Thanks to you and researches I found out how to do it ! For the others who wonder how to do I'll post how I did and what site I used to understand it !

So you just need to manipulate JSON with Java because your alfresco people page (127.0.0.1:8080/alfresco/service/api/people) returns a JSON object, and you'll be able to create, delete, search... users! Thx again !

Sites :

https://api-explorer.alfresco.com/api-explorer/#/people

http://crunchify.com/json-manipulation-in-java-examples/

The code : This is for creating an user :

public User createUser(String firstN, String lastN, String email, String pass, String authTicket) throws Exception{
        try{
            String url = "http://127.0.0.1:8080/alfresco/service/api/people?alf_ticket="+authTicket;
            HttpClient httpclient = new HttpClient();
            PostMethod mPost = new PostMethod(url);

            //JSONObject obj = new JSONObject();
            //JSONArray people = obj.getJSONArray("people");
            JSONObject newUser = new JSONObject();
            newUser.put("userName", firstN.toLowerCase().charAt(0)+lastN.toLowerCase());
            newUser.put("enabled",true);
            newUser.put("firstName",firstN);
            newUser.put("lastName", lastN);
            newUser.put("email", email);
            newUser.put("quota",-1);
            newUser.put("emailFreedDisable",false);
            newUser.put("isDeleted",false);
            newUser.put("isAdminAuthority",false);
            newUser.put("password", pass);

            //people.put(newUser);
            //Response response = PostRequest(newUser.toString())); 
            StringRequestEntity requestEntity = new StringRequestEntity(
                    newUser.toString(),
                    "application/json",
                    "UTF-8");
            mPost.setRequestEntity(requestEntity);
            int statusCode2 = httpclient.executeMethod(mPost);

            mPost.releaseConnection();
        }catch(Exception e){
            System.err.println("[ERROR] "+e);
        }
        return new User(firstN, lastN);
    }

And if you want to get all the users you have on alfresco :

public ArrayList<User> getAllUsers(String authTicket)
    {
        ArrayList<User> allUsers = new ArrayList<>();
        String lastName, firstName;
        try{
            String url = "http://127.0.0.1:8080/alfresco/service/api/people?alf_ticket="+authTicket;
            HttpClient httpclient = new HttpClient();
            GetMethod mPost = new GetMethod(url);
            int statusCode1 = httpclient.executeMethod(mPost);
            System.out.println("statusLine >>> "+statusCode1+"....."
                    +"\n status line \n"
                    +mPost.getStatusLine()+"\nbody \n"+mPost.getResponseBodyAsString());
            JSONObject obj = new JSONObject(mPost.getResponseBodyAsString());
            JSONArray people = obj.getJSONArray("people");
            int n = people.length();
            for(int i =0 ; i < n ; i++)
            {
                JSONObject peoples = people.getJSONObject(i);
                User u = new User(peoples.getString("firstName"), peoples.getString("lastName"));
                if (!allUsers.contains(u)){
                    allUsers.add(u);
                }
            }

        }catch(Exception e){
            System.err.println("[ERROR] "+e);
        }

        return(allUsers);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM